I have a string of text and within it are several instances that are always a hash and then a number (and then sometimes punctuation), eg:
Text #12321, Text #456 Text #789; Text #0! Etc.
I need regex to preg_match_all every number (variable lengths) following a hash but before anything not a number – so in the above example I’d get an array of:
‘12321‘ ‘456‘ ‘789‘ ‘0‘
I tried:
'/[#](.*?)[^0-9]/'
and
'/(\\d+)(.*?)(\\d+)/'
but the first failed in a bizarre way and the second only matches every other instance.
I’m trying to puzzle my way through documentation but it would help so much to see how best to do this. Could anyone save a me hours of confusion and tell me the correct regex syntax for this?
Use this regex: –
You don’t need to check for
non-numberat the end.\d+will only get you digits anyways, and will stop as soon as a non-digit is encountered.Note that
#is not a meta character in regex, so you don’t need to enclose it in a character class.