Java RegExp Replace - java

Hello I've been trying to make some replacement with not success
public class demo {
public static void main(String[] args){
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
Pattern pattern = Pattern.compile("/^\\/demoapi\\/api\\/user\\/([0-9]\\d*)$/i");
Matcher match = pattern.matcher(url);
}
}
I want to replace $1 with 123 , how do I do this ?!
Thank you !

I want to replace $1 with 123 , how do I do this ?!
Simply use replace method but never forget to escape $
"/user/?user=$1".replace(/(\$1)/,"123");

I think you are looking for something like this:
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
Pattern pattern = Pattern.compile(".*/user/(\\d*)");
Matcher match = pattern.matcher(url);
if(match.matches()){
newurl = newurl.replace("$1", match.group(1));
}
System.out.println(newurl);
Hope this helps.

You don't need to enter the whole text ^\\/demoapi\\/api\\/user\\ in the pattern. Just a ^.*\\/ will match upto the last / symbol. So Your java code would be,
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
String m1 = url.replaceAll("(?i)^.*\\/([0-9]+)$", "$1");
String m2 = newurl.replaceAll("\\$1", m1);
System.out.println(m2);
Output:
/user/?user=123
Explanation:
(?i) Turn on the case insensitive mode.
^.*\\/ Matches upto the last / symbol.
([0-9]+)$ Captures the last digits.
IDEONE
OR
String url = "/demoapi/api/user/123";
String m1 = url.replaceAll(".*/(\\d*)$", "/user/?user=$1");
You need to put / before (\\d*), so that it would capture the numbers from starting ie, 123. Otherwise it would print the last number ie, 3.

You can use any of the following method :-
public class Test {
public static void main(String[] args) {
public static void main(String[] args) {
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
String s1 = newurl.replaceAll("\\$1", Matcher.quoteReplacement("123"));
System.out.println("s1 : " + s1);
// OR
String s2 = newurl.replaceAll(Pattern.quote("$1"),Matcher.quoteReplacement("123"));
System.out.println("s2 : " + s2);
// OR
String s3 = newurl.replaceAll("\\$1", "123");
System.out.println("s3 : " + s3);
// OR
String s4 = newurl.replace("$1", "123");
System.out.println("s4 : " + s4);
}
}
Explanation of Methods Used :
Pattern.quote(String s) : Returns a literal pattern String for the
specified String. This method produces a String that can be used to
create a Pattern that would match the string s as if it were a
literal pattern. Metacharacters or escape sequences in the input
sequence will be given no special meaning.
Matcher.quoteReplacement(String s) : Returns a literal replacement
String for the specified String. This method produces a String that
will work as a literal replacement s in the appendReplacement method
of the Matcher class. The String produced will match the sequence of
characters in s treated as a literal sequence. Slashes ('\') and
dollar signs ('$') will be given no special meaning.
String.replaceAll(String regex, String replacement) : Replaces each
substring of this string that matches the given regular expression
with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl)
yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that backslashes () and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string; see Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String) to suppress the special
meaning of these characters, if desired.
String.replace(CharSequence target, CharSequence replacement) :
Replaces each substring of this string that matches the literal
target sequence with the specified literal replacement sequence. The
replacement proceeds from the beginning of the string to the end, for
example, replacing "aa" with "b" in the string "aaa" will result in
"ba" rather than "ab".

Compact Search: .*?(\d+)$
This is all you need:
String replaced = yourString.replaceAll(".*?(\\d+)$", "/user/?user=$1");
In the regex demo, see the substitutions at the bottom.
Explanation
(\d+) matches one or more digits (this is capture Group 1)
The $ anchor asserts that we are at the end of the string
We replace with /user/?user= and Group 1, $1

Related

regex for pattern in java

I need regex (named in code myRegex) which will match all functions (function1, function2, function3).
public static void main(String[] args) {
String template = "f[0-9]"; // like f1, f2 etc
String myRegex = "fun\\((" + template + "*)\\)"; //todo what regex?
Pattern myPattern = Pattern.compile(myRegex);
String function1 = "fun(f1)";
String function2 = "fun(f1,f9)"; //myRegex don't match
String function3 = "fun(f1,f9,f4)"; // myRegex don't match
List<String> functions = Lists.asList(function1, function2, function3);
for (String function : functions) {
Matcher matcher = myPattern.matcher(function);
while (matcher.find())
{
System.out.println(function + " match!");//works only for function1
}
}
}
Elements in brackets must be seperated by comma (,).
It must work for other funcions with many arguments like
:function4 = "fun(f1,f9,f4,f5,f7)";
Please use below.
String myRegex = "fun\\((" + template + ",)*" + template + "?\\)";
If you want to cater to fun() as well - without any parameters, use below
String myRegex = "fun\\((" + template + ",)*(" + template + ")?\\)";
Use the following regex pattern:
fun\(f[0-9](?:,f[0-9]){0,2}\)
This will match any function named fun() having between 1 and 3 f arguments. Your actual Java regex pattern should be defined as:
Pattern myPattern = Pattern.compile("fun\\(f[0-9](?:,f[0-9]){0,2}\\)");
Regex
Use the following regex. It will also work if there are spaces between arguments or parentheses:
fun\s*\(\s*(?:f[0-9])?(?:\s*,\s*f[0-9])*\s*\)
Demo
https://regex101.com/r/hndzov/1
Java string
Pattern myPattern = Pattern.compile("fun\\s*\\(\\s*(?:f[0-9])?(?:\\s*,\\s*f[0-9])*\\s*\\)")
Explanation
Regex
Description
fun
Match exactly the characters fun
\s
Match any whitespace character
\s*
Match 0 or more whitespaces
\(, \(
Match opening and closing parentheses
f
Match character f
[0-9]
Match any digit from 0-9
(?:)
Make a group but don't capture it
Cons
Also matches a parameter list with a leading comma like fun(,f2, f3).

Replace all characters between two delimiters using regex

I'm trying to replace all characters between two delimiters with another character using regex. The replacement should have the same length as the removed string.
String string1 = "any prefix [tag=foo]bar[/tag] any suffix";
String string2 = "any prefix [tag=foo]longerbar[/tag] any suffix";
String output1 = string1.replaceAll(???, "*");
String output2 = string2.replaceAll(???, "*");
The expected outputs would be:
output1: "any prefix [tag=foo]***[/tag] any suffix"
output2: "any prefix [tag=foo]*********[/tag] any suffix"
I've tried "\\\\\[tag=.\*?](.\*?)\\\\[/tag]" but this replaces the whole sequence with a single "\*".
I think that "(.\*?)" is the problem here because it captures everything at once.
How would I write something that replaces every character separately?
you can use the regex
\w(?=\w*?\[)
which would match all characters before a "[\"
see the regex demo, online compiler demo
You can capture the chars inside, one by one and replace them by * :
public static String replaceByStar(String str) {
String pattern = "(.*\\[tag=.*\\].*)\\w(.*\\[\\/tag\\].*)";
while (str.matches(pattern)) {
str = str.replaceAll(pattern, "$1*$2");
}
return str;
}
Use like this it will print your tx2 expected outputs :
public static void main(String[] args) {
System.out.println(replaceByStar("any prefix [tag=foo]bar[/tag] any suffix"));
System.out.println(replaceByStar("any prefix [tag=foo]loooongerbar[/tag] any suffix"));
}
So the pattern "(.*\\[tag=.*\\].*)\\w(.*\\[\\/tag\\].*)" :
(.*\\[tag=.*\\].*) capture the beginning, with eventually some char in the middle
\\w is for the char you want to replace
(.*\\[\\/tag\\].*) capture the end, with eventually some char in the middle
The substitution $1*$2:
The pattern is (text$1)oneChar(text$2) and it will replace by (text$1)*(text$2)

Java Regex to replace a pattern in a certain string

I want to replace a word starting with # in a string which contains set of words with the same word (# omitted)
example
"word1 word2 #user" should be replaced with "word1 word2 user"
Can someone help me?
You can use regex. Lets start with
yourText = yourText.replaceAll("#(\\S+)", "$1");
in regex:
\S represents any non-whitespace characters
+ represents one or more
\S+ represents one or more non-whitespace characters
(\S+) -parenthesis create group containing one or more non-whitespace characters, this group will be indexed as 1
in replacement
$1 in replacement allows us to use content of group 1.
In other words it will try to find #non-whitespaces (which and replace it with non-whitespaces part.
But this solution doesn't require # to be start of word. To do this we could check if before # there is
whitespace space \s,
or start of the string ^.
To test if something is before our element without actually including it in our match we can use look-behind (?<=...).
So our final solution can look like
yourText = yourText.replaceAll("(?<=^|\\s)#(\\S+)", "$1");
yes, String.replaceAll()
String foo = "#user"
foo = foo.replaceAll("#", "");
You have not very clear use case, but my assumptions with code example:
omit all symbols with replaceAll function
omit just first symbol with substring function
public class TestRegex {
public static void main(String[] args) {
String omitInStart = "#user";
String omitInMiddle = "#user";
String omitInEnd = "#user";
String omitFewSymbols = "#us#er";
List<String> listForOmit = Arrays.asList(omitInStart, omitInMiddle, omitInEnd, omitFewSymbols);
listForOmit.forEach(e -> System.out.println(omitWithReplace(e)));
listForOmit.forEach(e -> System.out.println(omitFirstSymbol(e)));
}
private static String omitFirstSymbol(String stringForOmit) {
return stringForOmit.substring(1);
}
private static String omitWithReplace(String stringForOmit) {
String symbolForOmit = "#";
return stringForOmit.replaceAll(symbolForOmit, "");
}
}

regex which should check string contains specified word

I wrote a regex which should check does string contains word 'Page' and after it any number
This is code:
public static void main(String[] args) {
String str1 = "12/15/14 7:01:44 Page 10 ";
String str2 = "12/15/14 7:01:44 Page 9 ";
System.out.println(containsPage(str2));
}
private static boolean containsPage(String str) {
String regExp = "^.*Page[ ]{1,}[0-9].$";
return Pattern.matches(regExp, str);
}
Result: str1: false, str2:true
Can you help me what is wrong?
Change the regex to the following:
String regExp = "^.*Page[ ]{1,}[0-9]+.$";
so that it matches one or more digits (hence the [0-9]+).
You also don't need the boundary matchers (^ and $) since Pattern#matches would match the entire input string; and [ ]{1,} is equivalent to [ ]+:
String regExp = ".*Page +[0-9]+.";
Change it to:
String regExp = "^.*Page[ ]{1,}[0-9]+.$"; //or \\d+
↑
[0-9] matches 9 in the second example, and . matches the space.
In the first example, [0-9] matches 1, . matches 0 and remained space isn't matched. Note that ^ and $ are not really needed here.
Your regex can be simplified to:
String regExp = ".*Page\\s+\\d+.";

How to replace a string in java if replacement string contains special(some regex) character

I am trying to replace a string in java where my replacement string contains special character exactly same as shown below.
String s1 = char(1)+"abc"+char(1);
String s2 = "!##$%^&*()-_`~";
String s3 = s.replaceAll(s1, s2);
Above written code throws java.lang.IllegalArgumentException: Illegal group reference
You should use regular replace instead replaceAll that takes regular expression.
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
Sample (http://ideone.com/HbV9eO)
String s= "\u0001abc\u0001more\u0001abc\u0001";
String s1 = "\u0001abc\u0001";
String s2 = "!##$%^&*()-_`~";
String s3 = s.replace(s1, s2);
System.out.println("new string = " + s3);
String.replace(CharSequence, CharSequence) as suggested in Alexei Levenkov's answer is the way to go if you want to replace a string with another string literally.
For the sake of completeness, if you want to do literal string replacement with replaceAll, you need to quote both the pattern string and the replacement string, so that they are treated literally (i.e. without any special meaning).
inputString.replaceAll(Pattern.quote(pattern), Matcher.quoteReplacement(replacement));
Pattern.quote(String) creates a pattern which matches the specified string literally.
Matcher.quoteReplacement(String) creates a literal replacement string for the specified string.

Categories

Resources