1

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?

4
  • why don't you use any XML parsing API? Commented Aug 29, 2014 at 19:11
  • 1
    ;&lt in char class doesn't mean the string ;&lt. It means match ; or & or l or t Commented Aug 29, 2014 at 19:12
  • 1
    The obligatory comment to all posts asking about using regex to parse XML: stackoverflow.com/a/1732454/18157 Commented Aug 29, 2014 at 19:19
  • @JimGarrison He doesn't want to parse XML with regex, he wants to store the regex pattern in an XML file... Not the same thing at all ;) EDIT: Well that was my first interpretation, now I'm not so sure anymore... the question isn't particularly clear. Commented Aug 30, 2014 at 0:05

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.