Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rexexp in java

 
Jon Stewart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
There is a useful section in the Java™ Tutorials, which you may or may not know about.
 
Jon Stewart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4318
234
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Spidy Manchen", please check your private messages regarding an important administrative matter.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the result of the first group via matcher.group(1).
 
Jon Stewart
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Christophe!
This was excactly what I was looking for!
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic