开发者

Regex matching into multiple groups per line?

I'm trying to come up with a regex to be able to parse the following type of strings into groups.

<link rel="alternate" type="application/rss+xml" title="[!PageTitle!]" href="[!SiteRoot!]/feed.xml" />

M开发者_如何学编程y regular expression is:

\[\!(.+)\!\]

The problem with this expression is that it seems to pull it all into 1 group:

Found 1 match:

  1. "[!PageTitle!]" href="[!SiteRoot!]" has 1 group:

     1. "PageTitle!]" href="[!SiteRoot"
    

I want dont want the regex to continue...am I missing a boundary? Ideally I want it to find 2 groups,

[!PageTitle!]

and

[!SiteRoot!]


try:

\[\!(.+?)\!\]

the + quantifier is greedy, so will match as far as possible in the line, capturing the first [ followed by the last ] in the line. +? is the non-greedy equivalent.

As an aside, I'm using Rad Software Regular Expression Designer. I like it.


Seems like this works ok :)

\[!(\w+)!\]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜