1

I have checked several other questions on SO like this, this, this, this, and this, but I can't seem to figure out my issue from any of these. It is quite likely that I'm missing something simple, but I don't know what.

I have a string like the following: This is some random text{colA}that needs{COLBB} to be matched.

I need to match on the A and the BB but nothing else. The rules are as follows

  • The prefix will only be {col (case insensitive)
  • The suffix will only be }
  • The characters to match will only be letters
  • The match must be case insensitive
  • The match will only be 1 or 2 characters
  • A word break can't be assumed before or after the prefix/suffix

I managed to get this: (?i:[a-z]{1,2}(?<={col[a-z]{1,2})(?=})) which matches {colaa} but not {cola} (In the case of {cola} it returns la when it needs to return a). I can't figure out how to tune this to work with the two different match cases.

I'm working in .Net and would really appreciate any help I could get on this matter. I feel so close, yet so far away!

Community
  • 1
  • 1
cjbarth
  • 4,189
  • 6
  • 43
  • 62

2 Answers2

1

The key is to move the lookbehind assertion to the beginning of the pattern, so we can specify the exact prefix, rather than having to assert on the prefix plus the matched part. So:

(?i:(?<=\{col)[a-z]{1,2}(?=\}))
ruakh
  • 175,680
  • 26
  • 273
  • 307
1

Try this:

(?i:(?<={col)[A-Za-z]{1,2}(?=}))
  • The whole expression is case insensitive: (?i: … )
  • We assert that the match must be preceded by "{col" with a lookbehind: (?<={col)
  • We match any one to two letters of any case: [A-Za-z]{1,2}
  • We assert that the match must be followed by "}" with a lookahead: (?=})
Jay
  • 56,361
  • 10
  • 99
  • 123