So, I've been trying to write out a regular expression for something like a resistance value, which contains a certain amount of digits and at most one letter, but is always a certain amount of characters total (let's use the example of a four-character resistance code).
First I could do '\d*[RKM]\d*'
but that would allow something like 'R'
.
Also, I could do something like '[\dRKM]{4}'
, but this will allow things like 'RRR4'
which is not a value that I would want.
'\d{1,4}[Rr]\d{0,3} | ([RKM]\d{3}) | (\d{4})'
, though more specific, would still allow '1234R567'
which is not four characters.
So basically, is there a more compact way of writing '[RKM]\d\d\d | \d[RKM]\d\d | \d\d[RKM]\d | \d\d\d[RKM] | \d\d\d\d'
?
String operations will always be faster than regular expression operations. Unless, of course, you write the string operations in an inefficient way. Regular expressions have to be parsed, and code generated to perform the operation using string operations.
Writing a regular expression pattern. A regular expression pattern is composed of simple characters, such as /abc/ , or a combination of simple and special characters, such as /ab*c/ or /Chapter (\d+)\. \d*/ . The last example includes parentheses, which are used as a memory device.
'a' (which in this case ?: is doing it is matching with a string but it is excluding whatever comes after it means it will match the string but not whitespace(taking into account match(numbers or strings) not additional things with them.)
The simplest regular expression is a single literal character. Except for the special metacharacters *+?()| , characters match themselves. To match a metacharacter, escape it with a backslash: \+ matches a literal plus character.
Depending on your regex flavor, you can use a lookahead:
^(?!(?:\d*\D){2})[\dRKM]{4}$
(?!(?:\d*\D)
- Assert that there aren't two non-digit characters.Or:
^(?=.{4}$)\d*(?:[RKM]\d*)?$
(?=.{4}$)
- Assert that the length of the string is 4.See also: Regular Expressions: Is there an AND operator?
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