Results 1 to 5 of 5
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default Check validation of the Regex

    Hi

    Is there any way to make sure that a certain regex is valid ?

    I give textfield with regex for user, but if user is filling "*" for example the program crash.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    Hi
    Check like this.I have demonstrated with the small piece of code.Just it checks the text for "*".if it finds then it prints invalid else valid.

    Go thru the below link for better understanding.
    Pattern (Java 2 Platform SE v1.4.2))

    import java.util.regex.*;
    class LogIn
    {
    public static void main(String args[])
    {

    String input = "sun*com";
    //Checks for the string which is having * or not
    Pattern p = Pattern.compile("\\*");
    Matcher m = p.matcher(input);
    if (m.find())
    System.err.println("invalid string.");
    else
    System.err.println("valid string");

    //Checks for email addresses that start with
    //www. and prints a message if it does.

    }//main
    }//class
    Ramya:cool:

  3. #3
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    Sorry but you miss-understand me

    I have text , tested text, let call it.

    I also have a regex string , which the user provides.
    if the text is
    abc1234
    and the user put in the regex
    abc[/d]*
    then we have a match
    but if user put in regex
    abcd+
    - no match.

    The problem is what the user put "bad regex" ?
    *abc*
    which is one example , and I'm sure there are more. ( the * in the begining makes it "bad" regex )
    on the function
    Java Code:
    text.matches(regexStr);
    I get these errors:
    Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
    *[dD]*
    ^
    at java.util.regex.Pattern.error(Pattern.java:1650)
    at java.util.regex.Pattern.sequence(Pattern.java:1787 )
    at java.util.regex.Pattern.expr(Pattern.java:1687)
    at java.util.regex.Pattern.compile(Pattern.java:1397)
    at java.util.regex.Pattern.<init>(Pattern.java:1124)
    at java.util.regex.Pattern.compile(Pattern.java:817)
    at java.util.regex.Pattern.matches(Pattern.java:919)
    at java.lang.String.matches(String.java:1921)

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Looking through java.util.regex I couldn't find anything which tested if the regular expression was well formed. The only suggestion I can give is to use a try-catch.

    That does seem like it would be a useful method to incorporate into the Pattern class.

    Mr. Beans

  5. #5
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    nice, try-catch seem to be the simplest solution. thanks

    How can I marked this topic as "Solved" ?

Similar Threads

  1. Interpretation of regex?
    By Ms.Ranjan in forum New To Java
    Replies: 2
    Last Post: 04-23-2009, 06:37 PM
  2. Multiline Regex
    By zriggle in forum New To Java
    Replies: 1
    Last Post: 02-26-2009, 04:49 AM
  3. Some help with regex and loop
    By moaxjlou in forum New To Java
    Replies: 21
    Last Post: 11-02-2008, 10:24 PM
  4. [SOLVED] More RegEx help
    By JT4NK3D in forum New To Java
    Replies: 2
    Last Post: 05-23-2008, 04:07 AM
  5. Regex pattern
    By ravian in forum New To Java
    Replies: 4
    Last Post: 12-11-2007, 10:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •