Matching exactly one occurrence in a string with a regular expression
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 :)
T开发者_Go百科he 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)
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 *$
^(?=[ 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.
^[^C]*C[^C]*$
but this will not verify the length of your string.
精彩评论