34

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?

4 Answers 4

27

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

1
  • This works perfectly fine. And I only want to match the beginning of a line, so I'll just keep the ^.
    – dnclem
    Commented Dec 22, 2011 at 19:53
4

Your solution looks pretty good. As an alternative you can try smth like that:

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

Btw, I think you want hyphen instead of underscore between 0 and 9, don't you?

3
  • Yeah, I did mean to use a hyphen instead of an underscore between 0 and 9. BTW, what does the "?:" mean in your regex?
    – dnclem
    Commented Dec 22, 2011 at 19:53
  • @david—?: means that the expression within the parenthesis is not captured. Check this site out if you don't know what I mean by that: regular-expressions.info/brackets.html
    – dlras2
    Commented Dec 22, 2011 at 19:57
  • @david, it's for non-capturing group as said above and it's more efficient then regular group.
    – KL-7
    Commented Dec 22, 2011 at 19:59
3

The solution you present is correct.

If you're trying to optimize the routine, and the number of matches strings matching 2 or more characters is much smaller than those that do not, consider accepting all strings of length 2 or greater, then tossing those if they're of length 3. This may boost performance by only checking the regex once, and the second call need not even be a regular expression; checking a string length is usually an extremely fast operation.

As always, you really need to run tests on real-world data to verify if this would give you a speed increase.

1
  • Alright, thanks for the tip. I'll try your method out. It sounds like a nice idea.
    – dnclem
    Commented Dec 22, 2011 at 19:51
0

so basically you want to match words of length either 2 or 2+2+N, N>=0

([A-Za-z0-9][A-Za-z0-9](?:[A-Za-z0-9][A0Za-z0-9])*)

working example:

#!/usr/bin/perl

while (<STDIN>)
{
    chomp;
    my @matches = ($_=~/([A-Za-z0-9][A-Za-z0-9](?:[A-Za-z0-9][A0Za-z0-9])*)/g);
    for my $m (@matches) {
        print "match: $m\n";
    }
}

input file:

cat in.txt
ab abc bcad a as asdfa
aboioioi i i abc bcad a as asdfa

output:

perl t.pl <in.txt
match: ab
match: ab
match: bcad
match: as
match: asdf
match: aboioioi
match: ab
match: bcad
match: as
match: asdf
2
  • 1
    -1, as A) it pains me to see duplicated expressions instead of {n}, and B) your solution only captures strings of even length (and even then doesn't do it particularly efficiently.)
    – dlras2
    Commented Dec 22, 2011 at 19:39
  • A) no comment, B) you've a sharp eye, thank you for noticing the error.
    – user237419
    Commented Dec 22, 2011 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.