File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex replacement

 
subba rao
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 240
2
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just remove the leading .*? from your regular expression. If it's not found with the regex, it's also not replaced.
 
subba rao
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 240
2
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 240
2
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic