Replacing occurrences of generic string - java

I never was good enough with regex, and I assume this is the job for it.
I have a link like www.somelink/phoyto.jpg/?sz=50
I need to replace 50 with my value, let's say 100. Trouble is, that I cannot be sure, that this will be always sz=50 and not sz=150 or sz=10 or any other value.
What I need is to find an occurence of string contains of 'sz' + number and replace it with 'sz=100'.
Sure, I can do that 'manually" in some for loop, but that wouldn't be nor smar nor efiicient.

str = "www.somelink/phoyto.jpg/?sz=50";
str.replaceall("sz=\\d+", "sz=100");
\d is the java pattern for digit. + stands for one or more digits. replaceall replaces all occurrences of sz=<number>.
Here is a handy online regex tester for java: http://www.regexplanet.com/advanced/java/index.html

This should work:
String link = "www.somelink/phoyto.jpg/?sz=50";
link = link.replaceFirst("sz=\\d+", "sz=100");
System.out.println(link);

It's pretty simple, and this pattern should work:
(sz=\d+)
Code:
String result = searchText.replaceAll("(sz=\\d+)", "sz100");
Example:
http://regex101.com/r/mB3xT9

Related

Java regex for inserting text between braces

I have a String
a = "stringWithBraces()"
I want to create the following string
"stringWithBraces(text)"
How do I achieve this using regex?
I tried this :
a.replaceAll("\\(.+?\\)", "text");
But get this :
stringWithBraces()
You can use lookaheads and do something like this:
(?<=\().*?(?=\))
Live Demo
Thus doing this:
String a = "stringWithBraces()";
a = a.replaceAll("(?<=\\().*?(?=\\))", Matcher.quoteReplacement("text"));
System.out.println(a);
Outputs:
stringWithBraces(text)
Note that in relation to replaceAll() then the replacement string has some special character. So you should most likely use Matcher.quoteReplacement() in order to escape those and be safe.
You can use this :
a = a.replaceAll("\\((.*?)\\)", "(text)");
You have to replace every thing between parenthesis with (text)
+ requires at least one char, the ? added here means the shortest match, so "...(.)...(.)..." would not continue to find ".)...(.".
a.replaceAll("\\(.*?\\)", "(text)");
You might have intended replaceFirst; though I think not.
You might also let the dot . match new line chars, for mult-line matches,
using the DOT_ALL option (?s):
a.replaceAll("(?s)\\(.*?\\)", "(text)");

replacing string with regex in java

I think I have a decent handle wrt matching strings using Regex in Java, but now I am trying to replace strings using Regex and not having much success.
Simply put, I am trying to find where there is a digit immediately followed by a constant string "CMR", then adding a space between the digit and the "CMR" substring. "0CMR" should become "0 CMR", "5CMR" should become "5 CMR", etc. Any preceding non-digit should be left as it was.
So my source string is "theStringThat0CMRhas"
my command is:
replaceAll("[0-9]CMR", "[0-9] CMR");
I get the added space in the result, but the result becomes "theStringThat[0-9] CMRhas" which obviously isn't what I need. Somehow I need to tell Regex not to replace with "[0-9]", but with whatever it matched on in the first place.
I know I'm doing this wrong, but I don't know what's right.
Any help appreciated.
Thanks,
Tom
You want to use a capturing group:
replaceAll("([0-9])CMR", "$1 CMR")
$1 references the first group in the match, denoted by parentheses.
Also, [0-9] can be substituted with \d.
Try this:
replaceAll("(?<=\\d)(?=\\D)"," ")
It uses look ahead for non digit character and negative look ahead for digit characters.
If you want just do it for the one with CMR after the digits, use:
"(?<=\\d)(?=CMR)"
You should group the number regex and call argument. Your code here:
replaceAll("([0-9])CMR", "$1 CMR");
For more regex knowledge, please read this document
https://www.tutorialspoint.com/java/java_regular_expressions.htm
Good luck!
a good starting point may be here for reading regex: http://www.regular-expressions.info/java.html
on this site the replacing string page is here: http://www.regular-expressions.info/replacetutorial.html
$with a number represents a whole regex match, and you can use these to refer to what you were doing
String testString = "theStringThat0CMRhas";
String resultString = testString.replaceAll("[0-9]CMR","$0");
System.out.println(resultString);
this would result in the answer: theStringThat0CMR has
you obviously didnt want this, so lets change the answer up a little
String testString = "theStringThat0CMRhas";
String resultString = testString.replaceAll("([0-9])CMR","$0 CMR");
System.out.println(resultString);
now we are referencing the parenthsis, in which it hasn't done anything yet, so its replacing what it found, with the same thing, a space, and CMR
your result would now be: theStringThat0CMR CMRhas
so lets reference the part where we have chosen the number
String testString = "theStringThat0CMRhas";
String resultString = testString.replaceAll("([0-9])CMR","$1 CMR");
System.out.println(resultString);
now your answer will be: theStringThat0 CMRhas
it is finding where it picked a number, replacing it with that number, a space, and then CMR
you are trying to do what I believe to be called a backreference though I am unsure. Regex is still not my strong suit either.

Regex to remove string between two characters (exclusive)

If I have a string blah.t!#Z8-st? how can I remove the string between the characters . and ? in Java?
so the resulting text would be blah.?
I have got the following but it is inclusive:
String s = "blah.t!#Z8-st?";
System.out.println(s.replaceAll("\\..*?\\?", ""));
Since this looks like a contest to "write the most complex regex", let me submit my modest contribution.
System.out.println("blah.t!#Z8-st?".replaceAll("\\..*?\\?", ".?"));
Update
After your edit it appears that you already have the exact same regex as mine, so the only thing you need to fix is the "replace with" part, where you keep the delimiters.
You can use this:
String repl = string.replaceAll("^(.*?\\.)[^?]*", "$1");
Use String replaceAll method with following pattern:
Pattern
(?<=\.).*(?=\?)
Code
String s = "blah.t!#Z8-st?asdas";
System.out.println(s.ReplaceAll("(?<=\\.).*(?=\\?)", ""));

how to remove only charactres in a given string in java?

I am trying to remove only [A-z|a-z] like this:
String input ="A021001208A 711100609C 01111";
String clean = input.replaceAll("\\D+^\\s+","");
System.out.println(clean.toString());
but the above code also removes the spaces; I don't want to remove space.
The expected output is:
021001208 711100609 01111
Please help me to formate the reg-ex to remove only characters.
Just replace [a-zA-Z] then:
String clean = input.replaceAll("(?i)[A-Z]+","");
(?i) is ignore case embedded flag expression.
Rather than use a positive character class, use a negated one. The regex you want is:
[^\d\s]
Which means "any character other than a digit or a whitespace".
When coded as java, it looks like:
String clean = input.replaceAll("[^\\d\\s]","");
Try this it will replace all occurence of alphabet from the given string.
String clean = input.replaceAll("[^a-zA-Z]", "");
You have to use [a-zA-Z] regular expression. So your .replaceAll() method will look like as below :
String clean = input.replaceAll("[a-zA-Z]","");

Problem replacing words using [^a-zA-Z] regex

Just could not get this one and googling did not help much either..
First something that I know: Given a string and a regex, how to replace all the occurrences of strings that matches this regular expression by a replacement string ? Use the replaceAll() method in the String class.
Now something that I am unable to do. The regex I have in my code now is [^a-zA-Z] and I know for sure that this regex is definitely going to have a range. Only some more characters might be added to the list. What I need as output in the code below is Worksheet+blah but what I get using replaceAll() is Worksheet++++blah
String homeworkTitle = "Worksheet%#5_blah";
String unwantedCharactersRegex = "[^a-zA-Z]";
String replacementString = "+";
homeworkTitle = homeworkTitle.replaceAll(unwantedCharactersRegex,replacementString);
System.out.println(homeworkTitle);
What is the way to achieve the output that I wish for? Are there any Java methods that I am missing here?
[^a-zA-Z]+
Will do it nicely.
You just need a greedy quantifier in order to match as many non-alphabetical characters you can, and replace the all match by one '+' (a - by default - greedy quantifier)
Note: [^a-zA-Z]+? would make the '+' quantifier lazy, and would have give you the same result than [^a-zA-Z], since it would only have matched only one non-alphabetical character at a time.
String unwantedCharactersRegex = "[^a-zA-Z]"
This matches a single non-letter. So each single non-letter is replaced by a +. You need to say "one or more", so try
String unwantedCharactersRegex = "[^a-zA-Z]+"

Categories