473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.MULTILINE as the second parameter to the compile method. Thanks.
Feb 8 '08 #1
7 1788
JosAH
11,448 Expert 8TB
Also add the flag Pattern.UNIX_LINES to the compile method.

kind regards,

Jos
Feb 8 '08 #2
BigDaddyLH
1,216 Expert 1GB
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.
Feb 8 '08 #3
Thanks for letting me know what way I have to present the question.

The code is as below:

Expand|Select|Wrap|Line Numbers
  1. import java.util.regex.*;
  2.  
  3. public class Main {
  4.  
  5.     /** Creates a new instance of Main */
  6.     public Main() {
  7.     }
  8.  
  9.     /**
  10.      * @param args the command line arguments
  11.      */
  12.     public static void main(String[] args) {
  13.                 String inputString = "jen@xqt.com\nnick.rob@gmail.com\nrunky.ss@yahoo.com";
  14.         String matchedPattern ="";
  15.         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][.,]?$";
  16.  
  17.  
  18.         Pattern p = Pattern.compile(regularExp);
  19.         Matcher m = null;
  20.         boolean matchFound;
  21.  
  22.         m = p.matcher(inputString);
  23.         matchFound = m.matches();
  24.         if (matchFound) 
  25.         {
  26.         matchedPattern += m.group()+"\n";
  27.         }
  28.         System.out.print(matchedPattern);
  29.     }
  30.  
  31. }
  32.  
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.
Feb 11 '08 #4
BigDaddyLH
1,216 Expert 1GB
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
Feb 11 '08 #5
BigDaddyLH
1,216 Expert 1GB
Your code works for me:

Expand|Select|Wrap|Line Numbers
  1. import java.util.regex.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         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][.,]?$";
  6.         String inputString = "jen@xqt.com\nnick.rob@gmail.com\nrunky.ss@yahoo.com";
  7.  
  8.         int flags = Pattern.UNIX_LINES | Pattern.MULTILINE;
  9.         Pattern pattern = Pattern.compile(regularExp, flags);
  10.         Matcher m = pattern.matcher(inputString);
  11.  
  12.         boolean found = false;
  13.         while (m.find()) {
  14.             System.out.format("group[%d, %d]=\"%s\"%n", m.start(), m.end(), m.group());
  15.             found = true;
  16.         }
  17.         if(!found){
  18.             System.out.println("No match found.");
  19.         }
  20.     }
  21. }
Feb 11 '08 #6
Yes, but it does not work if I give more data as below:

Expand|Select|Wrap|Line Numbers
  1. import java.util.regex.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         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][.,]?$";
  6.         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";
  7.  
  8.         int flags = Pattern.UNIX_LINES | Pattern.MULTILINE;
  9.         Pattern pattern = Pattern.compile(regularExp, flags);
  10.         Matcher m = pattern.matcher(inputString);
  11.  
  12.         boolean found = false;
  13.         while (m.find()) {
  14.             System.out.format("location[%d, %d]=\"%s\"%n", m.start(), m.end(), m.group());
  15.             found = true;
  16.         }
  17.         if(!found){
  18.             System.out.println("No match found.");
  19.         }
  20.     }
  21. }
  22.  
Feb 12 '08 #7
BigDaddyLH
1,216 Expert 1GB
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.
Feb 12 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
8
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:...
1
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...
11
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...
7
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...
1
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...
458
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...
25
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...
5
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,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.