Regex with lazy match (for first orccurance only) in Notepad++ - regex

I have this regex to find all lines starting from the word "chapter" till the first blank line: ^chapter.*^\s*$. However I want it to show the first occurrence only, so I tried adding to the end '.?' or '(.+?)'. But I an not sure how to implement the lazy quantifier here.
Example text:
Chapter 1: some text
more than one line,
next line.
Chapter 2: text text text
other text
Chapter 3: more text
more lines
more lines
So the regex should match from the first word "Chapter " till the blank line before the next chapter.. etc.

You can use" /Chapter((?!\n\n).)*/s, on windows Chapter((?!\R\R)(.|\R))*\R?
Chapter literal matches chapter beginning
((?!\n\n).)* matches any character as long as next two characters are not newlines (due to negative lookahead (?!\n\n))
note the s option that makes the dot . match a newline; If you don't have such option in notepad++, you can use Chapter((?!\n\n)(.|\n))* and on windows Chapter((?!\R\R)(.|\R))*\R? to match the newline.
Basic Demo
Windows demo

You may use this regex to select first set of lines starting with Chapter:
\AChapter.*\r?\n(?:.*?\S.*\r?\n)*
RegEx Demo
RegEx Details:
\A: Start anchor (matches once per document)
Chapter.*\r?\n: Match text Chapter followed by any text till line break
(?:.*?\S.*\r?\n)*: Match 0 or more following lines containing at least one non-space character

Related

Match Regex to a new line that has no characters preceding it

example text
example text
I was wondering if there was a way to match the line break in the middle of these two bits of text.
I was using \n but it would match at the end of "example text" and in the blank line
I am using this in a text to speech program called Voicedream to say out loud that it has progressed to a new line.
I suggest that you only match a newline that is preceded with another newline.
Use a positive lookbehind (?<=\n):
(?<=\n)\n
^^^^^^^

Multi-line regular expressions in Visual Studio Code

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

Eclipse Add text to first line of all files

I need to add text to first line of all my JSP's in eclipse, this is the regex I a using \A.* but some how it selects the first line, I just want to prepend text to the start of the file. any help will be very much appreciated.
The .* pattern matches any 0+ chars other than line break characters, so it matches the first line.
It seems that Eclipse Find/Replace regex feature does not match entirely zero-width patterns (e.g. (?=,) will not find and insert a text before commas).
A workaround is to match and capture some text with (...) (where ... stand for a consuming pattern) capturing group and use $1 in the replacement pattern to reinsert the matched text.
Use
\A(.*)
Replace with MY_NEW_TEXT_HERE_AT_THE_START_OF_FILE$1.

Regex to find all spaces in lines beginning with a specific string

I am searching for a regex to find all the spaces in lines starting with a specific string (in a SVN dump file). Despite the "global" modifier my regex returns only the first occurence of the space character.
A part of the file i am working on :
...
pla bla bli
Node-path: branches/BU ILD/ml_cf/syst em/Translation/TranslationManager.class.php
Node-kind: file
Node-action: change
Text-delta: true
....
The regex :
/Node-path: \S*(\ )/g
finds only the first space (between U and I) but not the others on the line.
Using PCRE regex to find all the spaces on a line starting with a particular text, use this regex:
/(?:^Node-path: |\G)\S+\K\h+/gm
RegEx Demo
Using (?:Node-path: |\G) we are matching lines starting with Node-path: OR positioning at the end of the previous match.
\G asserts position at the end of the previous match or the start of the string for the first match
\K resets the starting point of the reported match.
\h+ matches 1 or more of horizontal whitespace (space or tab)

Add to end of line that contains a specific word and starts with x

I would like to add some custom text to the end of all lines in my document opened in Notepad++ that start with 10 and contain a specific word (for example "frog").
So far, I managed to solve the first part.
Search: ^(10)$
Replace: \1;Batteries (to add ;Batteries to the end of the line)
What I need now is to edit this regex pattern to recognize only those lines that also contain a specific word.
For example:
Before: 1050;There is this frog in the lake
After: 1050;There is this frog in the lake;Batteries
You can use the regex to match your wanted lines:
(^(10).*?(frog).*)
the .*? is a lazy quantifier to get the minimum until frog
and replace by :
$1;Battery
Hope it helps,
You should allow any characters between the number and the end of line:
^10.*frog.*
And replacement will be $0;Batteries. You do not even need a $ anchor as .* matches till the end of a line since . matches any character but a line break char.
NOTE: There is no need to wrap the whole pattern with capturing parentheses, the $0 placeholder refers to the whole match value.
More details:
^ - start of a line
10 - a literal 10 text
.* - zero or more chars other than line break chars as many as possible
frog - a literal string
.* - zero or more chars other than line break chars as many as possible
try this
find with: (^(10).*(frog).*)
replace with: $1;Battery
Use ^(10.*frog.*)$ as regex. Replace it with something like $1;Batteries