简体   繁体   English

正确的时候匹配括号不应该

[英]Regex matching bracket when it should not

With the input: 随着输入:

"a[href*=\\"test\\"]" “一个[HREF * = \\” 测试\\ “]”

I am attempting to match the tag name from the following jQuery selector: 我试图匹配以下jQuery选择器中的标记名称:

Regex r = new Regex("^[A-z]+");
string tagName = r.Match("a[href*=\"test\"]").Value;

The issue is that it collects the left bracket instead of just A through z: 问题是它收集左括号而不是A到z:

tagName = "a[href" tagName =“a [href”

Why would it collect the bracket if it was not specified in the expression? 如果未在表达式中指定括号,为什么会收集括号?

It's because this 这是因为这个

[A-z]

Creates a character range from ASCII 'A' to ASCII 'z'. 创建从ASCII“A”到ASCII“z”的字符范围。 There are characters other than letters between Z and a, one of them being the [ character. Z和a之间有字母以外的字符,其中一个是[字符。 Use instead: 改为使用:

[A-Za-z]

To match the range from AZ and the range from az , but not the characters that fall in between them. 为了配合从范围AZ 范围az ,但并不表明他们之间落在字符。 You can look at an ASCII table to see the specifics, but the summary is that the characters (in their numerical ASCII order) look like this: 您可以查看ASCII表以查看详细信息,但摘要是字符(按其数字ASCII顺序)如下所示:

A, B, ..., Y, Z, [, \, ], ^, _, `, a, b, c, ..., z

You should try a tool like regexpal to test stuff like this. 你应该尝试像regexpal这样的工具来测试这样的东西。 Here's your example: 这是你的例子:

http://regexpal.com/?flags=g&regex= ^%5BA-z%5D%2B&input=a%5Bhref*%3D%5C%22test%5C%22%5D http://regexpal.com/?flags=g&rexx= ^%5BA-z%5D%2B&input = a%5Bhref *%3D%5C%22test%5C%22%5D

What's weird to me is the lower case "z" - for some reason, that is allowing the bracket to pass. 对我来说奇怪的是小写“z” - 由于某种原因,这允许括号通过。

Try this: 尝试这个:

^[^\[]+

http://regexpal.com/?flags=g&regex= ^%5B^%5C%5B%5D%2B&input=a%5Bhref*%3D%5C%22test%5C%22%5D http://regexpal.com/?flags=g&regex= ^%5B ^%5C%5B%5D%2B&输入= a%5Bhref *%3D%5C%22test%5C%22%5D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM