5 Replies Latest reply: Oct 11, 2012 1:39 AM by 818747 RSS

    Regex

    818747
      Hi ,

      Trying to write a regex for a code that is returned.

      If For Ex:
      1)This is the code returned.
      How will i write a Regex for identifying these type of code.
      1A Type: Air code 9856 Text: [some text ]

      2)Is there an option in Regex so that I can identify a code uniquely.
      EX:
      1A Type: Air code 9856 Text: [some text ]
      ---------Regex for this also matches
      1A Type: Air code 9235 Text: [some text ]

      Is Regex a solution for this or should try other option.
      import java.util.regex.*;
      public class RegExTest {
         public static void main(String args[]) {
            String pattern = "[0-9]A\\s[A-Za-z]{4}:\\s[A-Za-z]{3}\\s[A-Za-z]{4}\\s9856 Text: ";
            String text = "1A Type: Air code 9856 Text: [some text ]";
             Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(text);
            while (m.find()) {
                System.out.print("matched");
            }
        }
      }
       
      Edited by: vinTK on Oct 10, 2012 10:29 PM
        • 1. Re: Regex
          sabre150
          Looks to me like either you are missing the obvious or I don't understand your problem. A regex normally consists of text that matches the fixed patr of what you are looking for and then meta information to define the variable parts. You seem to be saying that in your case "1A Type: Air code 9856 Text: " is fixed so your regex is just
              String pattern = "1A Type: Air code 9856 Text: ";
           
          • 2. Re: Regex
            818747
            Hi Sabre,

            Text is not fixed,it will be varied.
            Find the sample code in comments as of various texts that can occur,
            I got the Regex working to some extent ,but it may satisfy more than one text as seen in code.

            Using Regex can i uniquely identify a code from my text.
            import java.util.regex.*;
            public class RegExTest {
               public static void main(String args[]) {
                  String pattern = "[0-9]A\\s[A-Za-z]{4}:\\s[A-Za-z]{3}\\s[A-Za-z]{4}:\\s[A-Za-z0-9]{4}\\s[A-Za-z]{4}:\\s[A-Za-z]?";
               String text = "1A Type: Air code: 9856 Text: [some text ]";
             // String text ="1A Type: Air code H&C Text: [Time Mandatory If Time Window Requested]";                    
            // String text = "1A Type: Air code: A08 Text:[Check Date]";                    
             // String text ="1A Type: Air Code: 9999 Text: [No Flight for this city pair - Enter a connect point /x…]";                    
              //String text ="CSX Type: Car Code: XX02013 Text: [Unknown Rental Station]";                    
            //  String text ="1A Type: Car Code: 534 Text: [Invalid Special Equipment Code-NSK]";                    
              //String text ="Type: CarList Code: XX02013 Text:[Unknown Rental Station][Could not find specified vendor location:Vendor:SX,Locationcode:HAMTHAMT01*]";                    
                   Pattern p = Pattern.compile(pattern);
                  Matcher m = p.matcher(text);
                  while (m.find()) {
                      System.out.print("matched");
                  }
              }
            }
            • 3. Re: Regex
              EJP
              So what is the exact text you are trying to match, and what is the exact result that you want to get out of it? Once you define that, the rest is easy. But just showing us your guesses at the answer doesn't get us anywhere.
              • 4. Re: Regex
                818747
                Is there a way where i can uniquely identify the code in my text using regex.
                String text = "1A Type: Air code: 9965 Text:[Check Date]";                    
                String text ="1A Type: Air Code: 9999 Text: [Enter a connect point]";
                In the above two can i uniquely identify 9965,9999 from my regex.

                Just need to retrieve the codes,from text can this be done using regex.
                • 5. Re: Regex
                  sabre150
                  vinTK wrote:
                  Is there a way where i can uniquely identify the code in my text using regex.
                  String text = "1A Type: Air code: 9965 Text:[Check Date]";                    
                  String text ="1A Type: Air Code: 9999 Text: [Enter a connect point]";
                  So your pattern is "1A Type: Air code: (\d{4}) Text:"
                  In the above two can i uniquely identify 9965,9999 from my regex.

                  Just need to retrieve the codes,from text can this be done using regex.
                  Which is just group 1 in your match!