Page 1 of 1

Regex help please

Posted: Sun Sep 21, 2014 3:18 am
by mgp22
Hi.
Given the string: X1aaX1bbX1ccY
I need to pull out anything between the last X1 and Y. Anything, of any length, is allowed in the aa, bb, or cc positions. I believe it will require a look-ahead or a look-behind, but I can't solve it. I have been pondering for hours. sigh.
e.g.
X1aaX1bbX1ccY should return "cc"
X1aaX1bbX1ccabcY should return "ccabc"
X1aaX1bbX1cc1Y should return "cc1"
etc...
Thanks in advance.

Re: Regex help please

Posted: Sun Sep 21, 2014 4:32 am
by twinedev
This worked for me:

Code: Select all

^.*X1(.*)Y$
Results from your 3 examples
[text] Start Length
Match 1: X1aaX1bbX1ccY 0 13
Group 1: cc 10 2
Match 2: X1aaX1bbX1ccabcY 15 16
Group 1: ccabc 25 5
Match 3: X1aaX1bbX1cc1Y 33 14
Group 1: cc1 43 3[/text]

Re: Regex help please

Posted: Sun Sep 21, 2014 12:58 pm
by mgp22
Thank you, thank you TwinDev! I'm going to study that and try to understand it. Not sure how it works...? Are the ^ and the $ necessary?