I have the following text below. How do I write the regex to capture all text related to REQ_XXX
?
This is my expression .*(REQ_.*)\|
, but it captures |3 |4 |5
. I just want it to stop after the first |
it sees.
I have the following text below. How do I write the regex to capture all text related to REQ_XXX
?
This is my expression .*(REQ_.*)\|
, but it captures |3 |4 |5
. I just want it to stop after the first |
it sees.
You can use :
.*(REQ_[^|]*)\|
The "|" character will be exclude.
Edit:
.*(REQ_.*?)\|
The trick is ?
tells preceding .*
to stop as early as possible. This way the first |
that follows terminates the scope of .*
. This is exactly what you want.
My old answer is below. It works well but I think the above is The Right Way.
Old answer:
.*
matches (almost) everything, including |
. Use [^|\n]*
to match everything but |
and newline.
.*(REQ_[^|]*)\|
\n
is needed because pure [^|]*
could span its match over multiple lines. I guess you don't want this.
Some of the solutions are already posted in other answers, I'm giving a summary here.
You can follow multiple approaches here, you need to decide based on your needs which one you use:
.*(REQ_[\w]+)[\s]*\|
[\w]+
matches all word characters (letters, numbers and "_")|
are not included in your captured result ([\s]*
is out of the brackets).*(REQ_[^|]+)\|
.*(REQ_.+?)\|
If REQ_
is allways followed by word characters, you can simply use REQ_w+
or if you don't want to match ABCREQ_123
, use a word boundary: \bREQ_\w+
.