Home > Back-end >  How to include the first character in a RegEx match from the RegEx pattern?
How to include the first character in a RegEx match from the RegEx pattern?

Time:10-29

I have the following text:

This is a test ::a. MODE 3 within 7 hours,

::b. MODE 4 within 13 hours, and

::c. MODE 5 within 37 hours

:: My Test

And the following RegEx pattern:

(?<=^::[A-z ])(.*?)(?=$)

Testing on here: https://regex101.com/r/w1f0Dn/1

It identifies the results that I need correctly (not the first line as it should). However, I'm trying to include the first character as part of the match for the results.

Example: the ::b. MODE line finds the line, but it's not including the b as part of the match. I need the final match to be b. MODE 4 within 13 hours, and vs .MODE 4 within 13 hours, and Is there a way to do this?

CodePudding user response:

Move the [A-Za-z ] group out of the lookbehind and into the 1st capture group: (?<=^::)([A-Za-z ].*)

CodePudding user response:

Maybe (?:^::\s*)(.*$) is enough. No need to use any look behind.

  • Related