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.MULTILINE as the second parameter to the compile method. Thanks.
7 1788
Also add the flag Pattern.UNIX_LINES to the compile method.
kind regards,
Jos
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.
Thanks for letting me know what way I have to present the question.
The code is as below: - 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\nnick.rob@gmail.com\nrunky.ss@yahoo.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(inputString);
-
matchFound = m.matches();
-
if (matchFound)
-
{
-
matchedPattern += m.group()+"\n";
-
}
-
System.out.print(matchedPattern);
-
}
-
-
}
-
When I enter the below lines in a TextArea, the request parameter gets submitted into a string as "jen@xqt.com\nnick.rob@gmail.com\nrunky.ss@yahoo.c om".
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.
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
Your code works for me: - 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\nnick.rob@gmail.com\nrunky.ss@yahoo.com";
-
-
int flags = Pattern.UNIX_LINES | Pattern.MULTILINE;
-
Pattern pattern = Pattern.compile(regularExp, flags);
-
Matcher m = pattern.matcher(inputString);
-
-
boolean found = false;
-
while (m.find()) {
-
System.out.format("group[%d, %d]=\"%s\"%n", m.start(), m.end(), m.group());
-
found = true;
-
}
-
if(!found){
-
System.out.println("No match found.");
-
}
-
}
-
}
Yes, but it does not work if I give more data as below: - 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\nnick.rob@gmail.com\nrunky.ss@yahoo.com my id is accc@b.com do you know e@3.co.in isdasdasd";
-
-
int flags = Pattern.UNIX_LINES | Pattern.MULTILINE;
-
Pattern pattern = Pattern.compile(regularExp, flags);
-
Matcher m = pattern.matcher(inputString);
-
-
boolean found = false;
-
while (m.find()) {
-
System.out.format("location[%d, %d]=\"%s\"%n", m.start(), m.end(), m.group());
-
found = true;
-
}
-
if(!found){
-
System.out.println("No match found.");
-
}
-
}
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: John Smith |
last post by:
Hello,
I have a rather odd question. My company is an all java/oracle shop.
We do everything is Java... no matter what it is... parsing of text
files, messaging, gui you name it. My question...
|
by: Michael McGarry |
last post by:
Hi,
I am horrible with Regular Expressions, can anyone recommend a book on it?
Also I am trying to parse the following string to extract the number
after load average.
".... load average:...
|
by: pawel |
last post by:
I have made some comparision C# to Java RegularExpression. The problem was
to find out if the rule match some text.
Matching were done for precompiled regular expressions, in 100000 iterations...
|
by: rajarao |
last post by:
hi
I want to remove the content embedded in <script> and </script> tags
submitted via text box.
My java script should remove the content embedded between <script> and
</script> tag.
my current...
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I...
|
by: passion_to_be_free |
last post by:
I am writing a javascript that will make an http request, sort through
the html for any links on the page, and then store them for future
processing.
To test things, I pasted html source code...
|
by: wellstone9912 |
last post by:
Java programmers seem to always be whining about how confusing and
overly complex C++ appears to them. I would like to introduce an
explanation for this. Is it possible that Java programmers...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: teo |
last post by:
I need to implement a boolean evaluation
in a Regular Expression like this:
(aaa AND bbb) OR (ccc AND ddd)
(see the #3 case)
- - -
1)
If I need to match a single word only,
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |