I am writting a REGEX to find sentences starting with _@_
and ending with __d__
. d
can be 1 or 2 digit.
For example, I have this expression
getting a regex _@_enter text text . tex, text here__2___2_ te xt text .
and would like to get
_@_enter text text . tex, text here__2__
Here is my try.
^(_@_)?.*\n\_\_\d{1,2}\_$/gm
Could someone please tell me what is wrong with this ?
CodePudding user response:
Try this
/(_@_)(. ?)(_\d{1,2}_)/gm
CodePudding user response:
If the match can be over 2 lines, you can optionally match the rest of the current line followed by a newline after matching _@_
.
_@_(?:.*\n)?.*?__\d\d?__
Explanation
_@_
Match literally(?:.*\n)?
Optionally match the rest of the line followed by a newline.*?
Match any character except a newline, as few as possible__\d\d?__
Match 1 or 2 digits between__
See a regex demo
const regex = /_@_(?:.*\n)?.*?__\d\d?__/g;
const str = `getting a regex _@_enter text text . tex, text here__2___2_ te xt text .
getting a regex _@_enter text text .
tex, text here__3___3_ te xt text .
getting a regex _@_enter text text .
tex, text here__4___4_ te xt text .`;
console.log(str.match(regex));
CodePudding user response:
A deviation from @Sect0R s answer that also matches sentences over multiple lines but doesn't need the s modifier:
(_@_)((?:.|\n) ?)(__\d{1,2}__)
(And it looks for two _ around the end-sentence-number.)