开发者

Regular expressions: Find and replace url with c:url tag

I have a problem to build good regular expressions to find and replace. I need to replace all urls in many .jsf files. I want replace ulrs staring by XXX开发者_开发百科 with <c:url value="URL_WITHOUT_XXX"/>. Examples below.

I stuck with find regular expression "XXX(.*)" and replace expression "<c:url value="\1"/>", but my find expression match to long string , for example "XXX/a" style="", but need that match only to first " (href end). Anybody helps ?

I have:

<a href="XXX/a" style="">
<a href="XXX/b" >
<a href="XXX/c" ...>

I want:

<a href="<c:url value="/a"/>" style="">
<a href="<c:url value="/b"/>" >
<a href="<c:url value="/c"/>" ...>

PS: Sorry for my poor english ;)

Edit: I use Find/Replace in Eclipse (regular expressions on)


You should specify the language you're working with.

The following regex will match what you want:

<a href="XXX[^\"]*"

If you want to have some particular value, you can group the regex according to your needs. For example:

<a href="(XXX[^\"]*)"

will give you in the first group:

XXX/a
XXX/b
XXX/b

If you want to have only /a, /b, and /c, you can group it like that:

<a href="XXX([^\"]*)"

Edit:
I will explain what <a href="XXX[^\"]*" does:

  • It will match: <a href="XXX
  • Then it should match anything that except a " zero or many times: [^\"]*
  • Finally match the ", which is not really necessary

When you do: [^abc] you're telling it to match anything but not a, or b, or c. So [^\"] is: Match anything except a ".

And the quantifier * means zero or more times, so a* will match either an empty string, or a, aa, aaa, ...

And the last thing: Groups

When you want to keep the value appart from the entire match, so you can do anything with it, you can use groups: (something).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜