Reputation: 53
I require a regex to match the string in the following way:
#1234abc
: Should get matched#abc123
: Should get matched#123abc123
: Should get matched#123
: Should not get matched#123_
: Should not get matched#123abc_
: Should get matchedThis 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
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
Reputation: 4159
You can use this Regex:
((.*?(\d+)[a-zA-Z]+.*)|(.*[a-zA-Z]+(\d+).*)).
Access it here: http://regexr.com/3ef6q
see it working:
Upvotes: 1