Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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

 
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
 
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





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.