How to select specific lines regardless on the content in the middle - regex

Let's start with the structure I have so that I can better explain what I want to do. Imagine that I have a text as it follows:
write information1/info_a/content
read information/content
write information1/info_b/content
write information1/info_c/content
write information2/info_a/content
write information2/info_b/content
write information3/format/info_b/content
I want to highlight every line that starts with a specific path and that also contains another path, for example:
starts with 'write'
and contains 'info_b'
The desired output with the give example above is then:
write information1/info_b/content
write information2/info_b/content
write information3/format/info_b/content
How can I do this with a regular expression?
Thanks to everybody in advance
I know that for selecting everything that starts with write with regex I can do:
^write
and that for saying until the end of the line I should use the key $

You can select any line containing "info_b" with the regex:
^write.*info_b.*$
Which translates to line start-> any amount of anything -> info_b -> any amount of anything -> end line

Related

Regular expression, get the rest of the text aswell

I have a text file, a regular expression that looks in that file and gets the things I want. I also write this new information into a new file, however not everything is written to the new file! The file that my regex reads from looks like this:
"This is my text, it contains of 53 or so words file. That is a very
good number. However 80 is a better number. Hopefully I can write more
words soon enough. Hopefully very very soon "
What is written to the new text file is:
"This is my text, it contains of 53 or so words file. That is a very
good number. However 80 is a better number. Hopefully I can write more
words"
I want everything to be written. Any ideas?
Without the regex you were using, it's impossible to say.
I would hazard a guess though, that what you need to do is stick .*$ on the end of the capture group, in order to grab the rest of the text on the line.
^[\s\S]*$
should do it for you.

Notepad ++ Regular Expression on finding missing {/}

I have one big file filled with custom text and scripts, that are used by one software, it crashes because of one problem
The software display whole text throught
{#HEXCOLOR}TEXT TEXT TEXT{/}
for example
{#FF00FF}Hello{/}
as we can see the whole text is inside custom script that starts from {#HEXCOLOR} and ends with {/} but in some lines the "{/}" is missing, that make program crash.
for example
{#FF00FF}Hello
It is possible some how to search for missing {/} in the file via Regular Expression ?
I tried by myself but failed:
{#[^{}]}.?{/[^{}]*}
you could use this pattern
({#[^}]+}[^{\r\n]+)(?={#|$)
and replace with \1{/}
Demo

Deleting comments from a file, using Regex

I want to write a program that deletes all the comments (starting with "//" until the end of the line) from a file.
I want to do it using regular expressions.
I tried this:
let mutable text = File.ReadAllText("C:\\a.txt")
let regexComment = new Regex("//.*\\r\\n$")
text <- regexComment.Replace(text, "")
File.WriteAllText("C:\\a.txt",text)
But it doesn't work...
Can you please explain to me why, and give me some suggestion to something that does work (preferable using regex..) ?
Thanks :)
Rather than loading the whole file into memory and running a regex on it, a faster approach that will handle any size file without memory issues might look like this:
open System
open System.IO
open System.Text.RegularExpressions
// regex: beginning of line, followed by optional whitespace,
// followed by comment chars.
let reComment = Regex(#"^\s*//", RegexOptions.Compiled)
let stripComments infile outfile =
File.ReadLines infile
|> Seq.filter (reComment.IsMatch >> not)
|> fun lines -> File.WriteAllLines(outfile, lines)
stripComments "input.txt" "output.txt"
The output file must be different from the input file, because we're writing to the output while we're still reading from the input. We use the regex to identify comment lines (with optional leading whitespace), and Seq.filter to make sure the comment lines don't get sent to the output file.
Because we never hold the entire input or output file in memory, this function will work on any size file, and it's likely faster than the "read entire file, regex everything, write entire file" approach.
Danger Ahead
This code will not strip out comments that appear after some code on the same line. However, a regular expression is not the right tool for that job, unless someone can come up with a regular expression that can tell the following two lines of code apart and avoid breaking the first one when you strip everything that matches the regex from the file:
let request = WebRequest.Create("http://foo.com")
let request = WebRequest.Create(inputUrl) // this used to be hard-coded
let regexComment = new Regex(#"//.*$",RegexOptions.Multiline)
Never mind, I figured it out. It should have been:
let regexComment = new Regex("//.*\\r\\n")
Your regex string seems to be wrong. "\\/\\/.*\\r\\n" worked for me.

PowerShell isolating parts of strings

I have no experience with regular expressions and would love some help and suggestions on a possible solution to deleting parts of file names contained in a csv file.
Problem:
A list of exported file names contains a random unique identifier that I need isolated. The unique identifier has no predictable pattern, however the aspects which need removing do. Each file name ends with one of the following variations:
V, -V, or %20V followed by a random number sequence with possible spaces, additional "-","" and ending with .PDF
examples:
GTD-LVOE-43-0021 V10 0.PDF
GTD-LVOE-43-0021-V34-2.PDF
GTD-LVOE-43-0021_V02_9.PDF
GTD-LVOE-43-0021 V49.9.PDF
Solution:
My plan was to write a script to select of the first occurrence of a V from the end of the string and then delete it and everything to the right of it. Then the file names can be cleaned up by deleting any "-" or "_" and white space that occurs at the end of a string.
Question:
How can I do this with a regular expression and is my line of thinking even close to the right approach to solving this?
REGEX: [\s\-_]V.*?\.PDF
Might do the trick. You'd still need to replace away any leading - and _, but it should get you down the path, hopefully.
This would read as follows..
start with a whitespace, - OR _ followed by a V. Then take everything until you get to the first .PDF

vim regex : multi-line edit and controlling the cursor position

A text file is formatted like this:
Section 4 Area B Unit 20
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
Section 589 Area C Unit 1005
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
and these sections repeat by the hundreds. The "stuff i don't need" lines are actually about 30 or so. I need to keep the association of the "Section..." line, "Title..." line and "line of text I need to keep" related to each other. So I was hoping to first destruct the text document down (linewise) to the stuff I need before operating on it further (character-wise). So I wrote this:
g!/\Section\s\d*\sArea\s\h\sUnit\s\d*\n\|^\s\{3}\zs\d*\s-\_.*\ze2010-11/d
After deleting I get the "Section.." line and the "Title..." line, but never the subsequent lines underneath the "Title.." line. Those subsequent lines vary from 4 to 8 lines, but the "2010-11" line is consistent and always what I no longer want.
You can see I tried using zs and ze to select what I do not want deleted. I think the selection is working because if I change the command to "2011-12" then there is no match and the (OR) half of the command does not return a result.
I think the fault might be the cursor position(?), but I'm not sure and my effort to fix that has failed.
Can anyone see my error?
Thanks!
Give this a whirl.
:silent! g/^Section/+ , /^\s\+\d\+ -/- d
:g/^\s\+2010/ , -/\nSection\|\%$/ d
:g finds every line matching start of the pattern, ! will revert the selection and command will get applied to these lines.
Would something like g/^Section.../normal! j2dd3jd} do?
If not you can use a search for the Title line inside normal!
You may need to enclose it in "exec" but may be much simpler to write a function.
Do you really need to use vim? Seems like job for Perl to me.
There are many ways to do t, I'm sure. I think this sequence of commands should work (ignoring comment lines that begin with double quote):
" global delete of line below 'Section' to line before 'Title'
g/^\s*Section/+1;/Title/-1delete
" global delete from date line to line before 'Section'
g/^\s*\d\d\d\d-\d\d/;/^\s*Section/-1delete
" go to top line of buffer
gg
" delete last chunk, from final date to last line
/^\s*\d\d\d\d-\d\d/;$delete