RegexKitLite 不匹配方括号
我正在尝试匹配文件中的用户名。有点像这样:
用户名=asd123 密码123
等等。
我正在使用正则表达式:
username=(.*) password
获取用户名。但如果用户名是 and[ers] 或类似的,则不匹配。它不会匹配括号。有什么解决办法吗?
I'm trying to match usernames from a file. It's kind of like this:
username=asd123 password123
and so on.
I'm using the regular expression:
username=(.*) password
To get the username. But it doesn't match if the username would be say and[ers] or similar. It won't match the brackets. Any solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会使用正则表达式:
username=([a-zA-Z0-9\[\]]+)password
或类似的东西。关于此的注意事项:
a-zA-Z0-9
跨越匹配字母数字字符(根据您的示例,这是 alphanumerc)。所以这将匹配任何字母数字字符或括号。+
修饰符可确保匹配至少一个字符。*
(Kleene star) 将允许零< /em> 重复,这意味着您将接受空字符串作为有效用户名。[:alnum:]
代替a-zA-Z0-9
。不过,如果不起作用的话,我上面给出的应该可以。或者,我会禁止用户名中包含括号。 IMO,它们并不是真正需要的。
I would probably use the regular expression:
username=([a-zA-Z0-9\[\]]+) password
Or something similar. Notes regarding this:
a-zA-Z0-9
spans match alphanumeric characters (as per your example, which was alphanumerc). So this would match any alphanumeric character or brackets.+
modifier ensures that you match at least one character. The*
(Kleene star) will allow zero repetitions, meaning you would accept an empty string as a valid username.[:alnum:]
in place ofa-zA-Z0-9
. The one I gave above should work if it doesn't, though.Alternatively, I would disallow brackets in usernames. They're not really needed, IMO.
您的正则表达式是正确的。相反,您可以尝试这个:
[
][[:alpha:]
] 表示]
和[
和[:alpha:]
包含在括号内。Your Regular Expression is correct. Instead, you may try this one:
[
][[:alpha:]
] means]
and[
and[:alpha:]
are contained within the brackets.