Results 1 to 5 of 5
Thread: Check validation of the Regex
- 05-25-2009, 01:23 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 99
- Rep Power
- 0
- 05-25-2009, 02:00 PM #2
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
}//classRamya:cool:
- 05-25-2009, 03:51 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 99
- Rep Power
- 0
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 isand the user put in the regexabc1234then we have a matchabc[/d]*
but if user put in regex- no match.abcd+
The problem is what the user put "bad regex" ?which is one example , and I'm sure there are more. ( the * in the begining makes it "bad" regex )*abc*
on the function
I get these errors:Java Code:text.matches(regexStr);
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)
- 05-25-2009, 05:12 PM #4
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
- 05-26-2009, 11:23 AM #5
Member
- Join Date
- Dec 2008
- Posts
- 99
- Rep Power
- 0
Similar Threads
-
Interpretation of regex?
By Ms.Ranjan in forum New To JavaReplies: 2Last Post: 04-23-2009, 06:37 PM -
Multiline Regex
By zriggle in forum New To JavaReplies: 1Last Post: 02-26-2009, 04:49 AM -
Some help with regex and loop
By moaxjlou in forum New To JavaReplies: 21Last Post: 11-02-2008, 10:24 PM -
[SOLVED] More RegEx help
By JT4NK3D in forum New To JavaReplies: 2Last Post: 05-23-2008, 04:07 AM -
Regex pattern
By ravian in forum New To JavaReplies: 4Last Post: 12-11-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks