Let’s say I have the following text:
abc123def
abc1234567890def
In this text, I want to JUST capture the “abc” and the “def” parts of the text, completely excluding the numbers. This could be done with the following expression: (abc)[0-9]*(def)
.
Quick Note before I move on: I’m aware that you could also use (D)
or something similar to achieve the same result. But for the sake of this example, pretend you cannot take advantage of the fact that the group I’m getting rid of isn’t the same character type as the rest of the word. This was just done to better illustrate my problem.
The problem with this, however, is that it splits “abc” and “def” into different capture groups. Thanks to the situation I’m using this regex for, I cannot use any code to merge the 2 reg-ex groups together. That means I’m trying to convert the reg-ex I already have to output abcdef
instead of abc
and def
.
I’ve tried…
(abc([0-9]*)def)
, hoping that the capture groups didn’t include the number string because it’s in its own capture group- Result:
abc123def
andabc1234567890def
- Result:
(abc(?=[0-9]*)def)
, hoping to “look ahead” the number string- Result:
no result
- Result:
(abc2)[0-9]*(def)
and(abc)[0-9]*(1def)
, hoping that the capture with the backreference would include the results from the other group- Result:
abc
for(abc2)[0-9]*(def)
andno result
for(abc)[0-9]*(1def)
- Result:
(abc(?:[0-9]*)def)
, because I misunderstood the documentation thinking that “non-capture group” meant that it would skip whatever’s inside it- Result:
abc123def
andabc1234567890def
- Result:
Nothing I’ve tried worked so far, either returning “abc” (no “def”) or nothing at all. So, how could I return “abc” and “def” while excluding the numbers between them all in the same capture group? I’m not sure if this matters, but the reg-ex used in this post was tested using https://regexr.com/ with the global
and multiline
expression flags checked.
2
If you need a pure regex solution, and your regex engine supports lookarounds, you could try this pattern:
(?<=abc)d+(?=def)
This pattern says to:
(?<=abc)
assert thatabc
precedesd+
then match one or more digits(?=def)
assert thatdef
follows the digits
Demo