简体   繁体   English

带哈希字符的正则表达式

[英]regular expression with hash character

I'm having trouble recognising a potential hash character. 我无法识别潜在的哈希字符。 I am using the following pattern that recognises files of the form: id-1321952010.xml. 我正在使用以下模式来识别以下格式的文件:id-1321952010.xml。 Some of these files may contain a # before the id therefore: #id-1321952010.xml need be picked up also. 这些文件中的某些文件可能在ID之前包含#,因此:#id-1321952010.xml也需要拾取。

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. 在边界之前,但无法使其正常工作,任何人都可以提供帮助。

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. 边界之前的值将不允许正则表达式匹配#id-1321952010.xml,因为在声明之前可能存在哈希之后(这是一个有冲突的规则),它将搜索字符串的开头( ^ )。

To allow for this, move the start-of-string delimiter to the beginning of the regex, outside of the word bound : 为此, 请将字串开头的分隔符移至regex的开头,而不是单词bound

^#?\\b[a-zA-Z]+\\-[0-9]{10,10}\\.[xml]{3,3}\\b$

(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. 根据@Mat的评论,如果要匹配字符串的开头和结尾,则可能根本不需要单词边界,因为它们变得多余了。

^#?[a-zA-Z]+\\-[0-9]{10,10}\\.[xml]{3,3}$

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

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