Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

regex: How to extract substrings?

Thread view: 
Markus Dehmann - 10 Dec 2005 05:11 GMT
This should be really easy, but I couldn't find it in the tutorials and
documentations on the web:

What is the Java equivalent to the following Perl operation?

my $entry = "__3432__Smith__"
my ($id,$name) =
     $entry =~ m/__(\d+)__([A-z]+)__/;

In other words, I want to extract the number and the name from the
string $entry, using a regular expression.

I tried the following:

Pattern p = Pattern.compile("__([0-9]+)__([A-z]+)__");
Matcher m = p.matcher("__3432__Smith__");
while(m.find()){
  System.out.println(m.group());
}

But that gives me the complete match at once, not the two subgroups that
I specified using the parens: (\d+) and ([A-z]+).

Who can help?
Thanks!
Knute Johnson - 10 Dec 2005 06:31 GMT
> This should be really easy, but I couldn't find it in the tutorials and
> documentations on the web:
[quoted text clipped - 21 lines]
> Who can help?
> Thanks!

From the docs on Matcher

"Capturing groups are indexed from left to right, starting at one. Group
zero denotes the entire pattern, so the expression m.group(0) is
equivalent to m.group()."

So to answer your question:

Pattern p = Pattern.compile("__(\\d+)__(\\w+)__");
Matcher m = p.matcher($entry);
if (m.matches()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

Signature

Knute Johnson
email s/nospam/knute/

IchBin - 10 Dec 2005 06:35 GMT
> This should be really easy, but I couldn't find it in the tutorials and
> documentations on the web:
[quoted text clipped - 21 lines]
> Who can help?
> Thanks!

Actually, Roedy has a nice reference for this

http://mindprod.com/jgloss/regex.html
Signature


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.