how can I convert xsd: pattern in java regex -


As I know, and I've used very little Java regex, a method for converting a control xsd ( Or tool): The pattern in Java regex

My XSD: The pattern is as follows:

   & lt; XSD: Pattern value = "[a-zA-z] {6} [0-9LMNPQRSTUV] {2} [a-zA-z] {1} [0-9LMNPQRSTUV] {2} [a-zA-Z] { 1} [0- 9 LmNPCRSTUV] {3} [AZA-ZED] {1} "/> & Lt; xsd: pattern value = "[0- 9] {11,11}" /> & Lt; / XSD: Ban & gt; & Lt; / XSD: simpleType & gt;    

You can load XSD in Java and remove the quote.

First load the XML program you need in the Java program (I called it CodexSchema.xsd ):

  DocumentBuilderFactory factory = DocumentBuilderFactory NewInstance (); DocumentBuilder Builder = Factory.Nude DocumentBluilder (); Document source = builder.parse (new file ("CodexSchema.xsd")); After that you can use XPath to find the patterns you want to remove (you want to create a method that takes simple type of name, if You have several procedures). To avoid registering names, I used a more complex XPath expression:  
  XPathFactory xPathfactory = XPathFactory.newInstance (); String type name = "myCodex"; String xPathRoot = "// * [local-name () = 'simpleType'] [[@ name = '+ +' Type name + ''] / * [local-name () = 'ban'] / * [local Name () = 'pattern'] "; XPath Pattern XPath = xPathfactory.newXPath (); // this & lt; x: Pattern & gt; The element of the node list indicates the element   

you can org.xml.dom.NodeList to and elements.

  nodelight pattern nodes = (nodelist) pattern XPath.evaluate (xPathRoot, source, XPathConstants.NODESET);   

Now you can loop through them and remove the content of your value attribute, you may want to write a method for it:

  public list & lt; Pattern & gt; GetPatterns (node ​​list patternnodes) {list & lt; Pattern & gt; Expression = new arseuse & lt; & Gt; (); (Int i = 0; i & lt; patternNodes.getLength (); i ++) {Element patternNode = (element) patternnets. Item (i); String regex = pattern Node.getAttribute ("value"); expressions.add (Pattern.compile (regex)); } Return expression; }   

You do not really need to put them in the pattern you can just use string .

Now you can use all your patterns in Java:

 for  (pattern p: getPatterns (patternNodes)) {System.out.println (p); }   

There are some tests with the third pattern:

  pattern pattern3 = getPatterns (patternNodes) .get (2); Matcher matcher = pattern3.matcher ("47385628403"); System.out.println ("test1:" + matcher.find ()); // print` test1: true` system.out.println ("test2:" + "47385628403" .matches (pattern3.toString ())); // print `test2: true`    

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -