How can I match letters a,b,c once in any combination and varying length like this:
The expression should match these cases:
abc
bc
a
b
bca
but should not match these ones:
abz
aab
cc
x
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
Matching a Single Character Using Regex By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.
To solve my problem I just tried this regex: "\d{1}[A-Z]{1}\d{1}" but it extract 9A1 or 9C1 given examples strings here above. You should be able to use a capture group to extract the letter you need.
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).
Use regex pattern
\b(?!\w*(\w)\w*\1)[abc]+\b
You can use this pattern with any set and size, just replace [abc]
with desired set...
Example:
(above output is from myregextester)
^(?=(.*a.*)?$)(?=(.*b.*)?$)(?=(.*c.*)?$)[abc]{,3}$
The anchored look-aheads limit the number of occurrences of each letter to one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With