Allena
Allena

Reputation: 53

Regex to match numbers only if alphabets are present

I require a regex to match the string in the following way:

This implies that it should only get matched if the string contains numbers or underscore along with alphabets. Only numbers/underscore should not get matched. Any other special characters should not get matched either.

This regex is basically to get hashtags from string. I have already tried the following but it didn't worked well for me.

preg_match_all('/(?:^|\s)#([a-zA-Z0-9_]+$)/', $text, $matches);

Please suggest something.

Upvotes: 3

Views: 187

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

If you need to match hashtags in the format you specified in a larger string, use

(?<!\S)#\w*[a-zA-Z]\w* 

See the regex demo

Details:

  • (?<!\S) - there must be a start of string or a whitespace before
  • # - a hash symbol
  • \w* - 0+ word chars (that is, letters, digits or underscore)
  • [a-zA-Z] - a letter (you may use \p{L} instead)
  • \w* - 0+ word chars.

Other alternatives (that may appear faster, but are a bit more complex):

(?<!\S)#(?![0-9_]+\b)\w+
(?<!\S)#(?=\w*[a-zA-Z])\w+

The point here is that the pattern basically matches 1+ word chars preceded with # that is either at the string start or after whitespace, but (?![0-9_]+\b) negative lookahead fails all matches where the part after # is all digits/underscores, and the (?=\w*[a-zA-Z]) positive lookahead requires that there should be at least 1 ASCII letter after 0+ word chars.

Upvotes: 1

You can use this Regex:

((.*?(\d+)[a-zA-Z]+.*)|(.*[a-zA-Z]+(\d+).*)).

Access it here: http://regexr.com/3ef6q

see it working:

enter image description here

Upvotes: 1

heemayl
heemayl

Reputation: 41987

Do:

^(?=.*[A-Za-z])[\w_]+$
  • [\w_]+ matches one or more of letters, digits, _

  • The zero width positive lookahead pattern, (?=.*[A-Za-z]), makes sure the match contains at least one letter

Demo

Upvotes: 0

Related Questions