java regular expressions problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rameshch45
    New Member
    • Oct 2007
    • 25

    java regular expressions problem

    I am reading a string from the TextArea content in a JSP page, and then building a pattern object then call compile method, it is not working beyond the first line though I passed Pattern.MULTILI NE as the second parameter to the compile method. Thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Also add the flag Pattern.UNIX_LI NES to the compile method.

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      If you are still having problems, post a SSCCE: http://physci.org/codes/sscce.html

      Note this problem has nothing to do with jsp or text areas! Your sample program will only be a few lines long.

      Comment

      • rameshch45
        New Member
        • Oct 2007
        • 25

        #4
        Thanks for letting me know what way I have to present the question.

        The code is as below:

        [CODE=Java]import java.util.regex .*;

        public class Main {

        /** Creates a new instance of Main */
        public Main() {
        }

        /**
        * @param args the command line arguments
        */
        public static void main(String[] args) {
        String inputString = "jen@xqt.com\nn ick.rob@gmail.c om\nrunky.ss@ya hoo.com";
        String matchedPattern ="";
        String regularExp = "^[a-zA-Z][\\w+.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z][.,]?$";


        Pattern p = Pattern.compile (regularExp);
        Matcher m = null;
        boolean matchFound;

        m = p.matcher(input String);
        matchFound = m.matches();
        if (matchFound)
        {
        matchedPattern += m.group()+"\n";
        }
        System.out.prin t(matchedPatter n);
        }

        }
        [/CODE]
        When I enter the below lines in a TextArea, the request parameter gets submitted into a string as "jen@xqt.com\nn ick.rob@gmail.c om\nrunky.ss@ya hoo.com".

        If I tokenize it using StringTokenizer and match it with the regular expression, all three ids are matched but three of them dont match when they are in a new line. I used the flags MULTILINE and UNIXLINES etc as the second parameter to the compile method but with no result.
        Last edited by BigDaddyLH; Feb 11 '08, 04:05 PM. Reason: added code tags

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Please enclose your posted code in [code] tags (See How to Ask a Question).

          This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

          Please use [code] tags in future.

          MODERATOR

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Your code works for me:

            [CODE=Java]import java.util.regex .*;

            public class Main {
            public static void main(String[] args) {
            String regularExp = "^[a-zA-Z][\\w+.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z][.,]?$";
            String inputString = "jen@xqt.com\nn ick.rob@gmail.c om\nrunky.ss@ya hoo.com";

            int flags = Pattern.UNIX_LI NES | Pattern.MULTILI NE;
            Pattern pattern = Pattern.compile (regularExp, flags);
            Matcher m = pattern.matcher (inputString);

            boolean found = false;
            while (m.find()) {
            System.out.form at("group[%d, %d]=\"%s\"%n", m.start(), m.end(), m.group());
            found = true;
            }
            if(!found){
            System.out.prin tln("No match found.");
            }
            }
            }[/CODE]

            Comment

            • rameshch45
              New Member
              • Oct 2007
              • 25

              #7
              Yes, but it does not work if I give more data as below:

              [CODE=Java]import java.util.regex .*;

              public class Main {
              public static void main(String[] args) {
              String regularExp = "^[a-zA-Z][\\w+.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w.-]*[a-zA-Z0-9].[a-zA-Z][a-zA-Z.]*[a-zA-Z][.,]?$";
              String inputString = "jen@xqt.com\nn ick.rob@gmail.c om\nrunky.ss@ya hoo.com my id is accc@b.com do you know e@3.co.in isdasdasd";

              int flags = Pattern.UNIX_LI NES | Pattern.MULTILI NE;
              Pattern pattern = Pattern.compile (regularExp, flags);
              Matcher m = pattern.matcher (inputString);

              boolean found = false;
              while (m.find()) {
              System.out.form at("location[%d, %d]=\"%s\"%n", m.start(), m.end(), m.group());
              found = true;
              }
              if(!found){
              System.out.prin tln("No match found.");
              }
              }
              }
              [/CODE]

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by rameshch45
                Yes, but it does not work if I give more data as below:
                I think that's because your pattern is "^...$" so it has to match an entire line.

                Comment

                Working...