Regex help please

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mgp22
Forum Newbie
Posts: 2
Joined: Sun Sep 21, 2014 3:12 am

Regex help please

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Regex help please

Post 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]
mgp22
Forum Newbie
Posts: 2
Joined: Sun Sep 21, 2014 3:12 am

Re: Regex help please

Post 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?
Post Reply