aspose file tools*
The moose likes Java in General and the fly likes regex replacement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regex replacement" Watch "regex replacement" New topic
Author

regex replacement

subba rao
Greenhorn

Joined: Apr 24, 2008
Posts: 24


final out put should be

http://xxx.xxx.xx.xx:8080/xxx-xxx-application/rservation-document/120-11-0
but am getting only 120-11-0;how can i get full url with replacement of the value.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

subba rao wrote:

.*?(\\d+)\\-(\\d+)\\-(\\d+)
for the above pattern .*? was not captured. Thus, disappeared.
You should wrap it with brackets as (.*?)
Because you have one more captured group, you should offset originals by 1
from $1-11-$3 to $1$2-11-$4

PS: you should only name constant (final variable with CAPITAL cases)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Or just remove the leading .*? from your regular expression. If it's not found with the regex, it's also not replaced.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
subba rao
Greenhorn

Joined: Apr 24, 2008
Posts: 24
thanks for the reply.

(.*?)

i know that if you we use .* then its the meaning of finding any single character with 0 or more times.then what is the meaning of (.*?)
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

subba rao wrote:thanks for the reply.

(.*?)

i know that if you we use .* then its the meaning of finding any single character with 0 or more times.then what is the meaning of (.*?)

What would you use the following ?
private static String REPLACE = "$1-11-$3";
Why $1? Why $3?
subba rao
Greenhorn

Joined: Apr 24, 2008
Posts: 24
Raymond Tong wrote:
subba rao wrote:thanks for the reply.

(.*?)

i know that if you we use .* then its the meaning of finding any single character with 0 or more times.then what is the meaning of (.*?)

What would you use the following ?
private static String REPLACE = "$1-11-$3";
Why $1? Why $3?


your code will also work.but my code also will work if we do like this.

private static String REGEX = "(.*?)\\-(\\d+)\\-(\\d+)"; // changed
private static String INPUT = "http://xxx.xxx.xx.xx:8080/xxx-xxx-application/rservation-document/120-521-0";
private static String REPLACE = "$1-11-$3"; // changed


INPUT = INPUT.replaceAll(REGEX,REPLACE);
System.out.println(INPUT);

so my question is what is the meaning of (.*?) and in which situations do we use this symbol?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Check out the Javadoc page of java.util.regex.Pattern for what it means. As for when to use it, when you need to match any number of characters but you don't want it to take up more than it should. For instance, if you have a regex of ".*(\\d-)+\\d", and an input of "abc1-2-3", the ".*" will not match "abc" as you may expect but "abc1-", leaving only "2-3" for the second part. If you use ".*?(\\d-)+\\d", the ".*?" will not steal the "1-" away and only match "abc", leaving "1-2-3" for the second part.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

subba rao wrote:
Raymond Tong wrote:
subba rao wrote:thanks for the reply.

(.*?)

i know that if you we use .* then its the meaning of finding any single character with 0 or more times.then what is the meaning of (.*?)

What would you use the following ?
private static String REPLACE = "$1-11-$3";
Why $1? Why $3?


your code will also work.but my code also will work if we do like this.

private static String REGEX = "(.*?)\\-(\\d+)\\-(\\d+)"; // changed
private static String INPUT = "http://xxx.xxx.xx.xx:8080/xxx-xxx-application/rservation-document/120-521-0";
private static String REPLACE = "$1-11-$3"; // changed


INPUT = INPUT.replaceAll(REGEX,REPLACE);
System.out.println(INPUT);

so my question is what is the meaning of (.*?) and in which situations do we use this symbol?

My question was actually helping you to find the answer.
I meant your REPLACE initialized as "$1-11-$3"
Why it is "$1 and $3" but not "$2 and $4" nor "$1, $2, $3"
 
 
subject: regex replacement
 
Similar Threads
Regular Expression: finding multiple lines
regExp matcher not working
problems with replaceAll and slash character
Convert from \uXXXX format to corresponding character
Java regex search. ".*" macther.find() or matcher.replaceAll, give more matches than intended