Question

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.

Was it helpful?

Solution

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}$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top