Can I specify I want the preceding character to be either (NOT A DIGIT) or (BEGINNING ANCHOR)?
I’m looking for “1/2”, but I want to ignore cases like 21/22. I know I can rule out preceding or trailing digits like this:
/([^0-9])1\/2([^0-9])/
But that fails when the match happens at the beginning or end of the line. Are anchors ^ and & allowed in grouping clauses?
If you also want to match something like
a1/2, you can use a negative lookbehind (provided that your regex implementation supports it):Which is “the string
1/2, not preceded by a digit”.If you can’t use lookbehind, you can almost literally transfer your requirements into a regex like
/([^\d]|^)(1\/2)/, which is “a character that is not a digit or the beginning of the phrase, followed by the string1/2(second capture group)”.