Expression to find a specific length string surrounded by constants
I do Regular Expressions so rarely tha开发者_如何学Got they always challenge me. Even simple ones.
How can I make a regular expression that will match all of these:
:= 'abc'
:= 'xyz'
:= '2rs'
:= 'abe'
:= 'a2c'
Basically it starts with := '
and ends with '
and has three values inside. Could be numbers or chars.
Something like this should work (as seen on rubular.com):
:= '([a-z0-9]{3})'
Explanation:
:= '
is matched literally since they're not metacharacters[a-z0-9]
defines a character class matching lowercase letters and digits{3}
is exact repetition, 3 times(...)
is a capturing group (not needed, but probably handy)
Minor variations on this pattern include:
[a-zA-Z0-9]
instead to also allow uppercase letters{1,3}
instead to allow between 1-3 repetition:= *'
instead to allow any number of space (*
here means "zero or more repetition of")
regular-expressions.info
- Character Classes, Repetition, Brackets for Grouping
- Flavor comparison - has information about differences between major regex flavors
精彩评论