1

How to match character in the text except the first occurrence?

Eg:

98C546CC456C67 should match 98C546CC456C67

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
kriznaraj
  • 484
  • 2
  • 11

3 Answers3

1

This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."

We can solve it with a beautifully-simple regex:

^[^C]*C|(C)

The left side of the alternation | matches the beginning of the string up to the first C. We will ignore this match. The right side matches and captures C to Group 1, and we know they are the right ones because they were not matched by the expression on the left.

This program shows how to use the regex (see the results at the bottom of the online demo):

var subject = '98C546CC456C67';
var regex = /^[^C]*C|(C)/g;
var group1Caps = [];
var match = regex.exec(subject);

// put Group 1 captures in an array
while (match != null) {
    if( match[1] != null ) group1Caps.push(match[1]);
    match = regex.exec(subject);
}

document.write("<br>*** Matches ***<br>");
if (group1Caps.length > 0) {
   for (key in group1Caps) document.write(group1Caps[key],"<br>");
   }

Reference

Community
  • 1
  • 1
zx81
  • 41,100
  • 9
  • 89
  • 105
1

Unfortunately, JavaScript's regex engine is severely limited. You can't do that in a single regex. The best solution probably would be to do

txt = subject.match(/[A-Z]/ig);  // or /[A-Z]+/ig, if CC should be a single match

and discard the first match.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

In a flavor that supports quantifiers in lookbehind like .NET Regex for example you can use lookbehind to look for preceding characters

foreach(Match m in Regex.Matches("98C546CC456C67", @"(?<=C.*?)C")){
    Console.WriteLine(m.ToString() + " at position " + m.Index);
}
Stilgar
  • 22,354
  • 14
  • 64
  • 101