Trim string using reqex match
I have to use a crippled tool which doesn't provide any way to trim leading an trailing spaces from a string. It does have .NET style regex, but only Match is implemented, not replace.开发者_高级运维 So, I came up (surprisingly by myself) with this regex that seems to work.. but I don't completely understand why it works :-)
$trimmed = regex/[^ ].*[^ ]/ ($original_string)
Why does this work, does it really work in all cases, and is there a better way if you only have regex Match ( even group matches can't be captured :( ) ?
It should work fine unless there's only a single character surrounded by space.
Your pattern searches for:
- A non-space character
[^ ]
- Zero or more characters of any kind, as many as possible (greedy match)
.*
- A non-space character
[^ ]
So, if there aren't at least two non-space characters (1 and 3), the pattern won't match at all.
You should use \b
instead of [^ ]
, that will match any 'word boundary', but will be of zero length and won't require two non-space characters:
\b.*\b
It works like this: [^ ]
will match the first non space character, .*
will match anything, and [^ ]
will again match a non space character. Since regex is greedy the longest possible match is returned, so in this case the longest possible string with two non spaces at the ends effectively trimming off whitespace at the beginning and end of $original_string
.
A good tutorial on regex is here, it teaches you about greedy and lazy matching which are key to understanding and optimizing regexes. It also teaches you about matching between characters which is what you would want to do here (see the answer about \b by Martin).
精彩评论