0

I'm trying to matching something like:

aabbaCaabaCabCbaCCC, grouped as (aabbaCaabaCabCba)(CCC), i.e. with any final Cs matched as a separate group.

I assumed I could use lazy matching somehow, but nothing I've tried worked. For example:

/(a+|b+|C*?)+(C*)/ and /(a+|b+|C*)+?(C*)/ match too much or not enough; they include the CCC in the first group, or they only match the initial aa.

This is in JavaScript by the way.

0

1 Answer 1

1

You can use this regex

\b([abC]*?)(C*)\b

Regex Demo

JS Demo

var re = /\b([abC]+?)(C*)\b/g;
var str = "aabbaCaabaCabCbaCCC";

if ((m = re.exec(str)) !== null) {
    document.writeln("<pre>" + m[1] + "</br>" + m[2] + "</br>" + "</pre>");
}

Sign up to request clarification or add additional context in comments.

7 Comments

Hmm, this one only works if I include ^ and $. Unfortunately I don't have that luxury since it's in the middle of a line...
@JosephHumfrey ok, but then think for yourself, which C will be the deciding factor?
@JosephHumfrey in aabbaCaabaC, I find two C..So one possibility is (aabba)(C)..is it wrong?
@JosephHumfrey It can be handled if its a word, though
Comment above from @WiktorStribiżew would be better here (:
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.