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.
java regular expressions problem
Collapse
X
-
Tags: None
-
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
-
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.Comment
-
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.
MODERATORComment
-
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
-
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
-
Originally posted by rameshch45Yes, but it does not work if I give more data as below:Comment
Comment