Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 26th, 2006, 06:54 AM
Registered User
 
Join Date: Nov 2005
Location: Munich, , Germany.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Regex question in Java

Hi list,

I am rewriting a Perl script in Java.

I am using java.util.regex.Pattern and java.util.regex.Matcher for regex.

My code looks like followig:

Code:
Pattern p = Pattern.compile("^S\\s+\\[(.*?)\\],\\s+C\\s+\\[(.*?)\\]\\[(.*?)\\]\\s+:\\s+\\(R (.+)\\)\\(V (.+)\\)(\\(A .+\\))?\\(U (.+)\\)\\s*\\[level    \\d+)\\].*$"
 
String text="S [Oct 18 17:30:36], C [10/18/06 5:30 PM][hardik] : (R 25891031)(V 1100)(A )(U 1100) [level:7][SelectDisplay] The list ContractList_FilterField : the length is 7"
 
Matcher m = p.matcher(text);
if(m.matches())
  for (int i=1; i<=5; i++)
       LOGGER.debug("grp "+i+ " value "+m.groups(i));
the output looks like this
Code:
    DEBUG - grp 0 value S [Oct 18 17:30:36], C [10/18/06 5:30 PM][hardik] : (R 25891031)(V 1100)(A )(U 1100) [level:7][SelectDisplay] The list ContractList_FilterField : the length is 7
    DEBUG - grp 1 value Oct 18 17:30:36
    DEBUG - grp 2 value 10/18/06 5:30 PM
    DEBUG - grp 3 value hardik
    DEBUG - grp 4 value 25891031
    DEBUG - grp 5 value 1100)(A  // look at this I expect this tobe only 1100
can anybody tell me what is wrong with the regex and what should I change to get 1100 in the 5th group ?

regards,
Hardik

Reply With Quote
  #2 (permalink)  
Old October 26th, 2006, 08:48 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
quote:
I am rewriting a Perl script in Java.
you have my sympathy ;)

Code:
(U (.+)\\)
The dot in that will match your bracket as well. Try:
Code:
(U (\d+)\\)
--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
Reply With Quote
  #3 (permalink)  
Old October 26th, 2006, 09:26 AM
Registered User
 
Join Date: Nov 2005
Location: Munich, , Germany.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

thanks.

That worked ! But I was talking about group 5 so

\\(V (.+)\\) needed to be chaned

and sometimes I also have strings with (V ?) so I have replaced it with

\\(V ([\\d?]+)\\)

and it worked.

Thanks for the idea.

cheers,
Hardik

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Saxon java question amhicraig XSLT 1 December 7th, 2007 01:21 PM
question in core java mrk_karupothu Java Basics 3 September 7th, 2007 10:07 AM
RegEx Question - Disregard dparsons ASP.NET 1.0 and 1.1 Professional 0 August 24th, 2006 09:30 AM
Java question hlchuah77 JSP Basics 2 October 5th, 2004 08:40 AM
A Java question hlchuah77 JSP Basics 0 September 27th, 2004 02:47 PM



All times are GMT -4. The time now is 10:37 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.