Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for n characters or at least m characters

This should be a pretty simple regex question but I couldn't find any answers anywhere. How would one make a regex, which matches on either ONLY 2 characters, or at least 4 characters. Here is my current method of doing it (ignore the regex itself, that's besides the point):

[A-Za-z0_9_]{2}|[A-Za-z0_9_]{4,} 

However, this method takes twice the time (and is approximately 0.3s slower for me on a 400 line file), so I was wondering if there was a better way to do it?

like image 337
rayanisran Avatar asked Dec 22 '11 19:12

rayanisran


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

Which pattern matches the preceding pattern at least n times but not more than m times?

The { n , m } quantifier matches the preceding element at least n times, but no more than m times, where n and m are integers. { n , m } is a greedy quantifier whose lazy equivalent is { n , m }? .


1 Answers

Optimize the beginning, and anchor it.

^[A-Za-z0-9_]{2}(?:|[A-Za-z0-9_]{2,})$ 

(Also, you did say to ignore the regex itself, but I guessed you probably wanted 0-9, not 0_9)

EDIT Hm, I was sure I read that you want to match lines. Remove the anchors (^$) if you want to match inside the line as well. If you do match full lines only, anchors will speed you up (well, the front anchor ^ will, at least).

like image 118
Amadan Avatar answered Sep 21 '22 05:09

Amadan