File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Rexexp in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Rexexp in java" Watch "Rexexp in java" New topic

Rexexp in java

Jon Stewart

Joined: Jul 12, 2010
Posts: 3
I'm new to java, but have programed some in perl.
In perl I could easy do things like:

This would output:
123 456

I have tried to do simular stuff in java, but I don't manage to collect the content of the paranteces.
I have tried using Pattern.comile and find and tried several other stuf. I maged to cauptare the complete regexp and getting the index values of start end end posision, but was not able to extract only the content of the paranteces.
Anyone that have an excample of how this is done?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Welcome to JavaRanch.

Java has no built-in support for regular expressions, so you cannot write something like /blabla/ for a regular expression. Also, the syntax for regular expressions in Java is not exactly the same as in Perl, so you probably can't use the exact same regular expression. The API documentation of class java.util.regex.Pattern describes Java's regular expression syntax.

You wrote that you already tried something with class Pattern. Why exactly did that not work? Could you get the source code to compile? If not, then what was exactly the error that you got? Can you show us your Java code that doesn't work?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Welcome again
There is a useful section in the Java™ Tutorials, which you may or may not know about.
Jon Stewart

Joined: Jul 12, 2010
Posts: 3
Yes, I looked at a tutorial of the java.util.regex, but did dot manage to find the way to capture the paranteces.
This is one excample of code that is compiling:

The input text have some text than a _ followed bye a number (several digits) ending with a . and some more text.
The %s contains the match of the complete regexp, so includes the starting _ and ending . outside the paranteces.
The %d gives the start index and %d.%n gives the ending.
From what I read \1 should give the first parantces, but aparantly Im not using it correct since it is not working.

Example output is:
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3915

"Spidy Manchen", please check your private messages regarding an important administrative matter.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You can get the result of the first group via

[My Blog]
All roads lead to JavaRanch
Jon Stewart

Joined: Jul 12, 2010
Posts: 3
Thank you Christophe!
This was excactly what I was looking for!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

One hint: declare and initialize the Pattern object outside the loop. That way you can share it for all your lines. (You will need a new Matcher each time though.)

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Rexexp in java
It's not a secret anymore!