I'm a regex newbie so this has been a lot of trial and error but for some reason I can only get this to work sometimes and I'm not sure why. Let me layout what I'm doing. I have a text file that looks like this:
1.Some Text Here
A paragraph of words here.
2.Some More Text Here
A paragraph of words here.
I use this code to find the lines with a number at the beginning:
^[0-9]+.([^.]*)$
Then I replace it with this:
<h2>$1</h2>\r\r
The problem I'm running into is that it usually grabs the line starting with the number but for some reason it will grab the line with the number and the paragraph below it. So instead of putting the </h2> at the end of the line it puts it at the end of the paragraph below.
I displayed all symbols to see if it had something to do with carriage/line returns but everything looks identical from line to line. The paragraph is on its own line and I see CRLF at the end of each line.
The expression [^.] (ie not a literal dot) matches newlines.
Don't match newlines in your capture:
^[0-9]+\.([^.\r\n]*)
Note that I also escaped the dot following the numbers, making it match a literal dot (a naked dot matches any character).
use \2 instead of $2, check "wrap around"tested on notepad++ 5.9.3 (UNICODE)
Not sure what version of notepad++ you're using but your version of the regex works fine for the example that you have ... i use 6.7.9.2
I can reproduce with the following text. Notice the paragraph for line 1 doesn't end in a period.
1.Some Text Here[CR][LF]
A paragraph of words here[CR][LF]
2.Some Text Here[CR][LF]
A paragraph of words here.[CR][LF]
Your regex matches any number of lines that begins with a set of digits, and doesn't end in a period. It could include more than one line. I would recommend this regex: ^[0-9]+\.([^\r\n]*)\r\n.
Related
I cannot figure a way to make regular expression match stop not on end of line, but on end of file in VS Code? Is it a tool limitation or there is some kind of pattern that I am not aware of?
It seems the CR is not matched with [\s\S]. Add \r to this character class:
[\s\S\r]+
will match any 1+ chars.
Other alternatives that proved working are [^\r]+ and [\w\W]+.
If you want to make any character class match line breaks, be it a positive or negative character class, you need to add \r in it.
Examples:
Any text between the two closest a and b chars: a[^ab\r]*b
Any text between START and the closest STOP words:
START[\s\S\r]*?STOP
START[^\r]*?STOP
START[\w\W]*?STOP
Any text between the closest START and STOP words:
START(?:(?!START)[\s\S\r])*?STOP
See a demo screenshot below:
To matcha multi-line text block starting from aaa and ending with the first bbb (lazy qualifier)
aaa(.|\n)+?bbb
To find a multi-line text block starting from aaa and ending with the last bbb. (greedy qualifier)
aaa(.|\n)+bbb
If you want to exclude certain characters from the "in between" text, you can do that too. This only finds blocks where the character "c" doesn't occur between "aaa" and "bbb":
aaa([^c]|\n)+?bbb
I have googled and checked several resources, but the last 2 hours of trial and error is no good.
I have many hundres of files in which I need to remove the first line of text
As for now I have this regular expression to get the first line of text:
(\A[^\n]*\n)
But I want to have a condition in my Regular expression, that the first line ALSO MUST contain this GLOBALS["\x61\156\x75\156\x61"]
Because not ALL first lines in every file should be replaced.
Is that possible?
To remove that first line containing the GLOBALS["\x61\156\x75\156\x61"] as a literal string, use
\A.*GLOBALS\["\\x61\\156\\x75\\156\\x61"].*\r?\n
Note that . matches any character but a newline, and \r? will also match Windows style line breaks (if you have any). Backslahses must be doubled if you need to match a literal backslash. The square bracket [ is also a regex metacharacter, and must also be escaped (\[).
I have an xml file that has a value like
JOBNAME="JBDSR14353_Some_other_Descriptor"
I am looking for an expression that will go through the file and change all of the characters in the quotes to Uppercase letters. Is there a Regex expression that will search for JOBNAME="Anything within the quotes" and change them to uppercase? Or a command that will find JOBNAME= and change all on that line to uppercase letters? I know that can just do a search for JOBNAME= and then use a VU command in vim to throw the line to uppercase store that to a macro and run that, but I was wondering if there was a way to get this done with a regex??
Here's an alternative with :substitute, as you had originally intended. This works better than #Zach's solution with gU_ when there's other text in the line:
:%s/JOBNAME="[^"]\+"/\U&/g
"[^"]\+" matches the quoted text (non-greedily by matching only non-quotes inside, to handle multiple quotes in a line)
\U turns the remainder of the replacement uppercase
for simplicity, the entire match (&) is uppercased here, but one could have also used capture groups (\(...\)), or match limiting with \zs
You can use the :g command which executes a command on lines that match a pattern:
:g/JOBNAME/norm! gU_
This will execute the gU_, which capitalizes all letters on a line, on all the lines that match JOBNAME
If there are other things on the same line that you don't want to capitalize, here is a solution for only the words in quotes:
:g/JOBNAME/norm! f"gU;
f" goes to the next quote. gU capitalizes with a motion. The motion used is ; which searches for the next " (repeats the last f command).
To do this with substitution you can use the \U atom which makes everything after it uppercase.
:%s/JOBNAME="\zs.*\ze"/\U&
\zs and \ze mark the start and end of the match and & is the whole match. This means that only the part between quotes is replaced.
In my quest to learn flex I'm having a scanner echo input adding line numbers.
After every line I display a counter and increment it.
Trouble is there is always a lone line number at the end of the display.
I need a regex that will ignore all line breaks except for the last one.
I tried [\n/<<EOF>>] to no avail.
Any thoughts?
I don't know what regex engine uses Flex but you can use this regex:
\z
Working demo
\z assert position at the very end of the string.
Matches the end of a string only. Unlike $, this is not affected by
multiline mode, and, in contrast to \Z, will not match before a
trailing newline at the end of a string.
If above regex doesn't work then you can use this one:
(?<=[\S\s])$
Working demo
Edit: since flex seems to work slightly different than other regex engines you could use this regex:
[\s\S]$
To get the latest character of each line. Then you can iterated over all lines until get the last one. Here you have an online flex regex engine tool:
http://ryanswanson.com/regexp/#start
Try below regex, It will search for a new line character at the end of the line.
\n$
Have you tried simply doing:
\n$
Debuggex Demo
The \n matches the newline, the $ matches end of string.
I'm trying to understand this as I'm reading tutorials and apply this to what I'm doing.
I have a file with lines of text like:
line1blahblahblahblah
line2blahblahblahblah
...
line10blahblahblahblah
I want to go in and remove the line and the number after it (which is incremented 1-1000 for each line) and replace it with new text leaving all the text after in tact.
Can someone explain how and explain the regex expression?
Search for
^line\d+
And replace with an empty string.
Explanation: The ^ matches the begining of the line, the line matches a literal character sequence, and the \d matches any digit character. The + after the \d makes it match one or more digits characters.
Your Notepad++ search panel should look like this: