5 Replies Latest reply: Nov 28, 2012 12:09 AM by 818747 RSS

    Regex

    818747
      Hi ,

      How can i write a Regex,where i can use the same order Time/Mandatory/Requested to match.
      How can i make sure that Time is first in my text.
      public class Test {
           public static void main(String[] args) {
           String text ="1A Type: Air code H&C Text: [Time Mandatory If Time Window Requested]";
           //Trying to match tha pattern for the text in the same order.
           //String pattern1="[Time]|[Mandatory]|[Requested] ";
      //     String pattern1="^.*Time.*$";//(?=.*?\bMandatory\b)(?=.*?\bRequested\b).*$";
           String pattern1="^(?=.*?\bTime\b).*$" ;
           Pattern p = Pattern.compile(pattern1);
           Matcher m = p.matcher(text);
           while (m.find()) {
                System.out.print("matched");
           }
           }     
      }
      {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Regex
          Darryl Burke
          Start a new thread for a new question. I've split this post from Regex

          db
          • 2. Re: Regex
            Darryl Burke
            Cross posted
            http://www.coderanch.com/t/598880/java/java/Regex

            db
            • 3. Re: Regex
              Darryl Burke wrote:
              Start a new thread for a new question.
              My Bad !

              The O.P. had done exactly that:
              Regex clarification
              but I'd advised them to keep their Regex inquiries with their earlier Regex inquiry.


              @ D.B.,
              Thanks for understanding their topic well enough to take care of this.

              @ O.P.,
              Good forum manners would require that you ALWAYS mention when you've cross-posted to other sites.
              • 4. Re: Regex
                818747
                could make it till here,

                but the order is not working

                Ex Strings:

                XXXXX Time XXXXXX Mandatory XXXXX Requested--Pass
                XXXXX Mandatory XXXXXX Time XXXXX Requested--fail(even this passed in my case:This should fail)

                Can i specify this in Regex like Time should be occured first,then some string followed by Mandatory
                and again some string follwed by Requested.
                public class Test {
                     public static void main(String[] args) {
                     String text ="1A Type: Air code H&C Text: [Time Mandatory If Time Window Requested]";
                    String pattern1="^(?=.*?Time)(?=.*?Mandatory)(?=.*?Requested).*$";
                     Pattern p = Pattern.compile(pattern1);
                     Matcher m = p.matcher(text);
                     while (m.find()) {
                          System.out.print("matched");
                                    
                     }
                     }     
                }
                Edited by: vinTK on Nov 27, 2012 9:45 PM

                Edited by: vinTK on Nov 27, 2012 9:54 PM
                • 5. Re: Regex
                  818747
                  db,

                  Posted here first,since no update posted same in ranch.


                  vinTK

                  Edited by: vinTK on Nov 27, 2012 10:09 PM