Im currenty having issues with a regex that Im creating. The regex has to extract all the groups that says number #### between Hello and Regards. At this moment my regex only extracts one group and I need all the groups inside, at this case I have 2, but there may be more inside.
I'm using the web page https://regex101.com/
Flavor: PCRE (PHP)
Regex: Hello\s.*(number\s*[\d]*)\s.*Regards
Text:
This is my test text number 25120
Hello my name is testing
I'm 20 years old
Please help me with the regex number 1542
I have been trying to create the regex many times this is my number 5152
Regards
I'm still trying my attempt number 5150
Result:
My Result is only the group number 5152 but inside is another group number 1542.
You may use
(?si)(?:\G(?!\A)|\bHello\b)(?:(?!\bHello\b).)*?\K\bnumber\s*\d+(?=.*?\bRegards\b)
See the regex demo.
Details
(?si)
- s
- DOTALL modifier making .
match any chars, and i
makes the pattern case insensitive(?:\G(?!\A)|\bHello\b)
- either the end of the previous match (\G(?!\A)
) or (|
) a whole word Hello
(\bHello\b
)(?:(?!\bHello\b).)*?
- any char, 0 or more times but as few as possible, that does not start a whole word Hello
char sequence\K
- match reset operator that discards all text matched so far\bnumber
- a whole word number
\s*
- 0+ whitespaces\d+
- 1+ digits(?=.*?\bRegards\b)
- there must be a whole word Regards
somewhere after any 0+ chars (as few as possible).