ibrahimyilmaz
ibrahimyilmaz

Reputation: 18909

Regular expression that finds a text between text and dot in Java

I'm crawling a web site and it has relative urls and I want to turn them to absolute path. For this reason, I want to use regular expression.

For example

 String text =  new String();
 text +="Lorem Ipsum /image/xyz.jpg"; 
 text +=" Lorem Ipsum /image/xyz2.jpg";
 text +=" Lorem Ipsum /image/xyz2.jpg";
 Pattern pattern = Pattern.compile("\\/image\\/.*\\.");
 Matcher matcher = pattern.matcher(text);
 while(matcher.find()){
  System.out.println(matcher.group());
 }

I want to get such kind of output:

/image/xyz.
/image/xyz2.
/image/xyz2.

I think my regex doesnt seem correct. How should I change it to work it properly.

Any help will be appreciated.

Upvotes: 1

Views: 293

Answers (2)

Raze
Raze

Reputation: 2224

The following should do the trick:

Pattern.compile("\\/image\\/[^.]*\\.");

This is assuming there's only one "." character in the match, ie, no multiple "."s

Upvotes: 2

Rui Jarimba
Rui Jarimba

Reputation: 17994

Try this regular expression:

/.*/\w+\.

For the following text:

Lorem Ipsum /image/xyz.jpg

It will match this

/image/xyz.

PS: I tested using .NET regular expressions, but this one should work fine in java

Upvotes: 1

Related Questions