2

I'd like to match a three-part string. The first part consists of one or more a characters, the second part consists of one or more b characters, and the third part consists either of zero or more c characters or zero or more C characters, but not a mix of c and C.

As such, I wrote the following regular expression:

/a+b+(C*|c*)/

And immediately noticed that it fails to greedily match the trailing cs in the following string:

aaaaabbcc

Wrapping the inner clauses of the or clause does not fix the unexpected behavior:

/a+b+((C*)|(c*))/

But interestingly both regular expressions match the following, where the C characters match the first clause of the or:

aaaaabbCC

The following regular expression captures the semantics accurately, but I'd like to understand why the original regular expression behaves unexpectedly.

/a+b+(([Cc])\2*)?/
2
  • Do you mean it does not work because the c is not matched? However, C* did, that is why c* did not even get tested. Commented Aug 7, 2016 at 20:41
  • Ah, yes - I should clarify that Commented Aug 7, 2016 at 20:42

3 Answers 3

5

Your regex doesn't work because first it tries C*, which matches the empty string, so it has satisfied the or clause. Then it won't try to check if c* can match more characters.

Here's a regular expression which does match the string as intended:

/a+b+(C+|c+)?/

That is, if it finds a C it will match as many more C as possible, if it finds a c it will match as many more c as possible. But finding C or c is optional.

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

Comments

2

You have to place the * outside of the bracket!

1 Comment

No, that would match aaaaabbcCcCc, which I do not want.
1

var input = "aaaaabbc";

// if you want to pick up c
console.log(/a+b+(c|C)*/.exec(input).pop());

4 Comments

No, that would match aaaaabbcCcCc, which I do not want.
Yep, I realized it was a little lacking in detail. The point is that the regex appears to be treating the two clauses of the or clause inconsistently.
If you have a critique of my question, please make a specific comment. I have read that page in the past, and merely linking to it does not help me improve my question.
I don't understand what you are looking for, probably you need to pick up the c, probably all the string, probably the c could be optionally matched... I can't help you, the question is your, so, you need to clarify what you want.

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.