1

I am having trouble coming up with the proper regular expression to match 3-character strings.

I have this string:

asdasa asdas asdhhgfh dfgdfdasa ttte aa1 asasda aa2

I'm trying to use regex, but can't manage to craft the proper regular expression to match substrings 3-characters long.

I'm currently trying to tweak this one - ^.{2}$, but it's not returning what is expected and I'm getting the error of missing delimiter "^".

0

3 Answers 3

5

Something like this. Your string is in the 2nd capturing

(^|\s)+(.{3})($|\s)+

Explanation

  • /(^| )(.{3})($| )/g

    • 1st Capturing group Capturing group (^|\s)+

      Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]

      • 1st Alternative: ^

        • ^ assert position at start of the string
      • 2nd Alternative: \s

        • \s match any white space character [\r\n\t\f ]
    • 2nd Capturing group (.{3})

      • .{3} matches any character (except newline)

        Quantifier: {3} Exactly 3 times

    • 3rd Capturing group ($| )+

      Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]

      • 1st Alternative: $

        $ assert position at end of the string

      • 2nd Alternative: \s

        • \s match any white space character [\r\n\t\f ]
    • g modifier: global. All matches (don't return on first match)

1

Following a (black magic) example in Perl to extract all the substrings of 3 letters

perl -ne 'while(/(\w)(?=(\w\w))/g){print "$1$2\n" }'
1

First, because there is no code implementing your regex, there is no way for us to be certain that this question pertains to Ubuntu (is on-topic).

Second, I'll explain the error message because it can be a little misleading to developers who are encountering it for the first time. It is telling you that you failed to finish your pattern with another caret (^). The truth is, if you merely add a caret to the end of your pattern it will damage the intended logic of your pattern. You see, the code that you are writing is expecting your pattern to be wrapped in delimiting characters - forward slashes are the most commonly used delimiters. To correct the syntax, just add a slash at the start and a slash at the end of your pattern.

Third, your pattern logic needs to be corrected. You don't want to use the beginning of string (^) and end of string ($) metacharacters for your needs. The solution is quite simple and straightforward with the use of "word boundary" meta characters (\b). An online demonstration

/\b\w{3}\b/

No capture groups or lookarounds are necessary. \w is a "word" metacharacter that matches [a-zA-Z0-9_] which means any character in the following list: lowercase letters, uppercase letters, numbers, and underscore. The {3} is a "quantifier" which demands that exactly 3 characters in the \w range are matched. The \b on either side of the \w{3} ensures that the there is no character that could be matched by \w immediately before or after the 3 matched characters.

Your sample input string doesn't look very realistic, so it is hard to confidently offer a pattern which executes tighter validation.

If you are new to regular expressions, this is a nice resource to read over: https://www.rexegg.com/regex-php.html

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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