F
Fritz Bayer
Hello,
the following code snippet:
String replacementPattern = "-$1 $2-";
Pattern patter = Pattern.compile("http://(.*?)/(.*)");
pattern.matcher("http://www.example.com/test/index.html").replaceFirst(replacementPattern);
pattern.matcher("http://www.example.com/").replaceFirst(replacementPattern);
pattern.matcher("http:///").replaceFirst(replacementPattern);
will generate the following output:
-www.example.com /test/index.html-
-www.example.com -
- -
What I would like to achieve is to print out a constant infront of
each capturing group, if and only if, the group is not empty.
In other words: I'm looking for a value of "String
replacementPattern", which will generate the following output for the
GIVEN code above:
-host:www.example.com uri:/test/index.html-
-host:www.example.com -
--
I only know how to always print something infront of each capturing
group, regardless of whether or not it was empty ( String
replacementPattern = "- host:$1 uri:$2-"
.
Fritz
the following code snippet:
String replacementPattern = "-$1 $2-";
Pattern patter = Pattern.compile("http://(.*?)/(.*)");
pattern.matcher("http://www.example.com/test/index.html").replaceFirst(replacementPattern);
pattern.matcher("http://www.example.com/").replaceFirst(replacementPattern);
pattern.matcher("http:///").replaceFirst(replacementPattern);
will generate the following output:
-www.example.com /test/index.html-
-www.example.com -
- -
What I would like to achieve is to print out a constant infront of
each capturing group, if and only if, the group is not empty.
In other words: I'm looking for a value of "String
replacementPattern", which will generate the following output for the
GIVEN code above:
-host:www.example.com uri:/test/index.html-
-host:www.example.com -
--
I only know how to always print something infront of each capturing
group, regardless of whether or not it was empty ( String
replacementPattern = "- host:$1 uri:$2-"
Fritz