Search code examples
phpregexexpressionpreg-matchpreg-match-all

match "#" followed by numbers" using regular expressions php


I have this in a string. And need to get "USING REGULAR EXPRESSIONS" not explode not substr.. the words that follows "#123" or "#345345" the numbers vary in lenght.

String = "address number between #258 Kentucky";

In that case get Kentucky. I have used this but doesn't work.

\b#([0-9]+)\b

Solution

  • You cannot use \b (word boundary) before # which is a non-word character.

    Just use this regex:

    #([0-9]+)\s+(\w+)\b
    

    RegEx Demo