Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better Way to Write Regular Expression

Tags:

regex

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'?

like image 374
Marcus Gladir Avatar asked Jul 17 '13 13:07

Marcus Gladir


People also ask

Is there anything faster than regex?

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.

How do I write regular expressions?

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.

What is the use of ?: In regex?

'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.)

What is the simplest regular expression?

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.


1 Answers

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?

like image 163
Kobi Avatar answered Sep 21 '22 07:09

Kobi