aspose file tools*
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
Author

Rexexp in java

Jon Stewart
Greenhorn

Joined: Jul 12, 2010
Posts: 3
Hi!
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: 13875
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

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

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:
Match
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2885
    
  13

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


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You can get the result of the first group via matcher.group(1).


[My Blog]
All roads lead to JavaRanch
Jon Stewart
Greenhorn

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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rexexp in java
 
Similar Threads
Apache problems
Regular expression
reading in a csv file
Beginner Java frustrations from a Perl Programmer
Perl Vs Java