Regex: Match opening/closing chars with spaces
I'm trying to complete a regular expression that will pull out matches based on their opening and closing characters, the closest I've gotten is
^(\[\[)[a-zA-Z.-_]+(\]\])
Which will match a string such as "[[word1]]" and bring me back all the matches if there is more than one, The problem is I want it to pick up matchs where there may be a space in so for example "[[word1 word2]]", now this will work if I add a space into my pattern above however this pops up a problem that it will only get one match for my entire string so for example if I have a string
"Hi [[Title]] [[Name]] [[surname]], How are you"
then the match will be [[Title]] [[Name]] [[surname]]
rather than 3 matches [[Title]]
, [[Name]]
, [[surname]]
. I'm sure I'm just a char or two away in the Regex but I'm stuck, How can 开发者_如何学JAVAI make it return the 3 matches.
Thanks
You just need to make you regex non-greedy by using a ?
like:
^(\[\[)[a-zA-Z.-_ ]+?(\]\])
Also there is a bug in your regex. You've included -
in the char class thinking of it as a literal hyphen. But -
in a char class is a meta char. So it effectively will match all char between .
(period) and _
(underscore). So you need to escape it as:
^(\[\[)[a-zA-Z.\-_ ]+?(\]\])
or you can put is in some other place in the regex so that it will not have things on both sides of it as:
^(\[\[)[a-zA-Z._ -]+?(\]\])
or
^(\[\[)[-a-zA-Z._ ]+?(\]\])
You need to turn off greedy matching. See these examples for different languages:
- asp.net
- java
- javascript
You should use +?
instead of +
.
The one without the question mark will try to match as much as possible, while the one with the question mark as little as possible.
Another approach would be to use [^\]]
as your characters instead of [a-zA-Z.-_]
. That way, a match will never extend over your closing brackets.
精彩评论