0

I'm currently learning regular expressions, and I'd appreciate any help understanding this:

Suppose I have a string with leading and trailing whitespace:

        abc      

and I would like to isolate the string while removing all of the whitespace. My idea is to use:

\s*(\w+)\s*

as the * quantifier, being greedy, will take as much of the leading and trailing whitespace as it can, which leaves me with just the string "abc". This approach seems to work.

However, I have seen some solutions have the ^ and $ anchors included, giving:

^\s*(\w+)\s*$

Why is it necessary to include the ^ and $ anchors? I know their function, however I can't see why the \s* preceding and following the (\w*) is not sufficient.

1
  • Think of what would happen to the string " a b c "
    – Bergi
    Commented Nov 4, 2014 at 1:26

3 Answers 3

1

Because in mulitine mode, you need to use anchors to match the leading and trailing spaces or otherwise it would match also the newline character from the previous line if the input contains two or more lines. If you want to match the horizontal spaces only, then i would recommend \h instead of \s. Most programming languages won't support \h

  • \s*(\w+)\s* would capture foo, bar in this foo bar string.
  • ^\s*(\w+)\s*$ won't match this foo bar string
1
  • Thanks Avinash. I see what you mean, but if my only concern is purely isolating the text without the whitespace, is it ok for me to still use my above code? As in, I don't mind what I match, I'm only concerned about what I capture.
    – Mel
    Commented Nov 4, 2014 at 1:20
1

Why is it necessary to include the ^ and $ anchors? Well, anchors define a position in the string where a match must occur. When you are using anchors in your pattern the regular expression engine does not move forward through the string or consume any characters. It looks for a match in those positions only.

This says whitespace must occur at the beginning of the string only.

^       # assert position at the beginning of the string         
\s*     # whitespace (\n, \r, \t, \f, and " ") (0 or more times)

This says whitespace must occur at the end of the string only.

\s*     # whitespace (\n, \r, \t, \f, and " ") (0 or more times)
$       # assert position at the end of the string 
0

Be careful on using the *

  • means ZERO or MORE times. This can be dangerous, because it can produce a match even where there are no actual characters. For example, this ^\s*(\w*)\s*$ will produce this match.

which is NOT what you want. Since * is ZERO or MORE than zero, it will find \w zero times (meaning there isn't any \w) so it will match it! Kind of confusing, I know.

Why include ^ and $?

Let's take your example \s*(\w+)\s* and use it with and without the ^ and the $. Let's suppose we're trying to match abc in " abc & " (without the quote marks). As you will see from this link, it will successfully match the abc together with the spaces and omit the &.

Let's try the same text, the same, regex just with ^ and $ (see this example). No match is produced! But why? Basically, putting a regular expression between ^ and $ is saying: I want ONLY the thing(s) that are between the ^ (beginning of the line) and $ (end of the line). If you find anything that is NOT these things, don't match it.

Regarding your expression, you'll certainly capture abc but match it along with the white-spaces. If you want to only match abc, just type \w+ which is going to match the string (\w = A-Z, a-z, 0-9 and _) and nothing else.

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.