简体   繁体   中英

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. 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.

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 :

^#?\\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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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