Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching exactly one occurrence in a string with a regular expression

Tags:

regex

The other day I sat with a regular expression problem. Eventually I solved it a different way, without regular expressions, but I would still like to know how you do it :)

The problem I was having was running svn update via an automated script, and I wanted to detect conflicts. Doing this with or without regex is trivial, but it got me thinking about a more obscure problem: How do you match exactly ONE occurrence of a character inside a fixed length field of whitespace?

For instance, let's say we wanted to match "C" inside a six-byte wide field:

"C     " MATCH
"   C  " MATCH
" C C  " NO MATCH
"  M   " NO MATCH
"      " NO MATCH
"C      " NO MATCH (7 characters, not 6)
"   C " NO MATCH (5 characters, not 6)
like image 704
KennethJ Avatar asked Oct 21 '10 09:10

KennethJ


2 Answers

I know it's not right to answer your own question, but I basically merged your answers ... please don't flame :)

^(?=.{6}$) *C *$

Edit: Replacing . with Tomalak's response below [ C] increases the speed with about 4-5% or so

^(?=[ C]{6}$) *C *$

like image 120
KennethJ Avatar answered Oct 23 '22 05:10

KennethJ


^(?=[ C]{6}$) *C(?! *C)

Explanation:

^             # start-of-string
(?=[ C]{6}$)  # followed by exactly 6 times " " or "C" and the end-of-string
 *C           # any number of spaces and a "C"
(?! *C)       # not followed by another C anywhere (negative lookahead)

Notes:

  • The ^(?=…{6}$) construct can be used anywhere you want to measure string length but not actually match anything yet.
  • Since the end of the string is already checked in the look-ahead, you do not need to put a $ at the end of the regex, but it does not hurt to do it.
like image 31
Tomalak Avatar answered Oct 23 '22 04:10

Tomalak