Márcio
Márcio

Reputation: 57

Java regex to xml

I did the following regular expression in java:

(^(?!\\s+$).*[^\\/:*?\"<>|]+(\\.(?i)(txt|rtf|doc|docx|htm|html|pdf))$)

But i need to use it in a xml schema, so i've changed for:

(^(?!\s+$).*[^\/:*?\&quot;&lt;&gt;|]+(\.(?i)(txt|rtf|doc|docx|htm|html|pdf))$)

But it's version is accepting files with not listed extensions. What's wrong?

Upvotes: 1

Views: 323

Answers (2)

Amulya Koppula
Amulya Koppula

Reputation: 174

Only for .xml files extension matching, I have sent regular expression as a string argument as below,

String xmlRegex=new String(".*\\.(xml)");

and verifies as below,

boolean matched = MatcherUtil.match( xmlRegex, filename );

It returns true if matched and false if not.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163585

Your regular expression shouldn't match anything: for a start, "^" and "$" are not meta-characters in the XSD regex dialect, they match literal "^" and "$" characters. There are also many other constructs here that XSD regexes don't allow. However, you may be using a schema processor such as the Microsoft one that does it's own thing rather than following the W3C specification.

It would be easier for us if you described your requirement, rather than asking us to work it out by reverse engineering a complex regular expression. Don't forget that you can specify more than one pattern. If you just want to require one of the specified extensions, just use

.*\.(txt|rtf|doc|docx|htm|html|pdf)

Upvotes: 3

Related Questions