开发者

How to capture any character using regular expressions

I want to capture text in an attribute within an XML tag. That is

<tag1 name="tag^*&,+">

I want to capture the value within the name attribute (which in this case would be tag^*&,+). This regular expression

name=\"([a-z开发者_开发技巧0-9]+)\"  

will only return the value if the text in the attribute is alphanumeric. Is there any syntax that will return the captured value regardless of what symbol and characters? Thanks!


At the risk of beating a dead horse, don't try to "parse" XML with regular expressions. Use your programming language's XML library. It is then dead simple to select all tag1 elements and get the contents of their name attributes.

Not only is it easier for you to code, but you won't have to deal with nasty things like strings spanning multiple lines, string escapes (e.g. &quot;), weird edge cases that cause your regex to fail, etc.


Check out regular-expressions.info

This will do what you want:

([^"]+)


You should use:

name=\"([^\"]+)\"

In other words, the capturing group can be described as at least one of "any character other than the end quotation"


It seems that your better of using an XML Parser I don't know what language your using but there's an XML parser for every language out there.


. will match any character.

name = \"(.+)\"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜