I’ve got a large chunk of text, in that text there is several occurrences of text like this 201014120 - 10. There is also text with normal hyphens (-).
What I’m trying to do, is to replace the hyphen between the two numbers with a comma , but not replacing any other hyphens in the text. I’d like to do this with PHP’s preg_replace.
The numbers are not the same and the length of each numbers will not be the same for all the text.
I’ve tried different types of regex expressions, both with the code and with this very nice website.
You can use lookaround to make sure that you match only hyphens surrounded by digits:
(?<=\d)is positive lookbehind: it will only allow the regex to match if it’s preceded by a digit. Conversely,(?=\d)is positive lookahead. The regular expression will match (and therefore replace) any whitespace surrounding the hyphen.