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)
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:
^(?=…{6}$)
construct can be used anywhere you want to measure string length but not actually match anything yet.$
at the end of the regex, but it does not hurt to do it.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With