带有哈希字符的正则表达式
我无法识别潜在的哈希字符。我使用以下模式来识别以下形式的文件:id-1321952010.xml。其中一些文件可能在 id 之前包含 #,因此:还需要选取 #id-1321952010.xml。
目前,对于我的初始情况:
QRegExp rxLogFileFormat("\\b^[a-zA-Z]+\\-[0-9]{10,10}\\.[xml]{3,3}$\\b");
我尝试添加“#?”在边界之前但无法使其正常工作,任何人都可以提供帮助。
I'm having trouble recognising a potential hash character. I am using the following pattern that recognises files of the form: id-1321952010.xml. Some of these files may contain a # before the id therefore: #id-1321952010.xml need be picked up also.
Presently for the initial case I have:
QRegExp rxLogFileFormat("\\b^[a-zA-Z]+\\-[0-9]{10,10}\\.[xml]{3,3}$\\b");
I've tried adding '#?' before the boundary but cannot get it to work correctly, can anyone assist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地在边界前添加
#?
将不允许正则表达式匹配 #id-1321952010.xml,因为它会在您之后搜索字符串的开头 (^
)已经声明它前面可能有一个散列,这是一个冲突的规则。为此,将字符串开头分隔符移至正则表达式的开头,位于单词边界之外 :
(还将字符串结尾分隔符移到单词边界之外)
另外,
根据@Mat的评论,如果您匹配字符串的开头和结尾,则可能不需要单词边界无论如何,因为它们变得多余。
Simply adding
#?
before the boundary will not allow the regex to match #id-1321952010.xml, because it will search for the start of the sting (^
) after you've declared that there may be a hash before it, which is a conflicting rule.To allow for this, move the start-of-string delimiter to the beginning of the regex, outside of the word bound:
(also moved the end-of-string delimiter outside of the word bound for good measure)
Also,
Based on @Mat's comment, if you're matching the start and end of a string, you probably don't need the word bounds at all, as they become redundant.