How to replace all substrings? - java

I want to replace all the text within brackets to uppercase letters for any String object. For example if the text is - Hi (abc), how (a)re (You)?" , output should be - Hi ABC, how Are YOU? . I tried to use StringUtils.SubstringBetween(), but that replaces only the first substring between ().
Using regex, I suppose the group() method requires the count of such substrings. What is the correct direction to be taken?

Since Java 9 we can use Matcher.replaceAll​(Function<MatchResult,String> replacer)
String str = "Hi (abc), how (a)re (You)?";
Pattern p = Pattern.compile("\\((.*?)\\)"); //matches parenthesis and places
//content between them in group 1
String replaced = p.matcher(str)
.replaceAll(match -> match.group(1).toUpperCase()); //replace each match with
//content of its group 1 in its upper case
System.out.println(replaced);
Output: Hi ABC, how Are YOU?

Related

How to split a List of Strings in Java with a regex that uses the last occurrence of given Pattern?

I'm pretty new to the regex world.
Given a list of Strings as input, I would like to split them by using a regex of punctuations pattern: "[!.?\n]".
The thing is, I would like to specify that if there are multiple punctuations together like this:
input: "I want it now!!!"
output: "I want it now!!"
input: "Am I ok? Yeah, I'm fine!!!"
output: ["Am I ok", "Yeah, I'm fine!!"]
You can use
[!.?\n](?![!.?\n])
Here, a !, ., ? or newline are matched only if not followed with any of these chars.
Or, if the char must be repeated:
([!.?\n])(?!\1)
Here, a !, ., ? or newline are matched only if not followed with exactly the same char.
See the regex demo #1 and the regex demo #2.
See a Java demo:
String p = "[!.?\n](?![!.?\n])";
String p2 = "([!.?\n])(?!\\1)";
String s = "I want it now!!!";
System.out.println(Arrays.toString(s.split(p))); // => [I want it now!!]
System.out.println(Arrays.toString(s.split(p2))); // => [I want it now!!]
s = "Am I ok? Yeah, I'm fine!!!";
System.out.println(Arrays.toString(s.split(p))); // => [Am I ok, Yeah, I'm fine!!]
System.out.println(Arrays.toString(s.split(p2))); // => [Am I ok, Yeah, I'm fine!!]
The split() methos of String accept as delimitator a regular expression.
Ref.: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
Example:
String str = "Am I ok? Yeah, I'm fine!!!";
String delimiters = "[!.?\n]";
String[] splitted = str.split(delimiters);
for(String part : splitted) {
System.out.print(part + "\n");
}
Output:
Am I ok
Yeah, I'm fine

How do we replace below 'x' sub string with 'y' sub string using regex in Java?

[![enter image description here][1]][1]I want to replace substring src="/slm/attachment/63338424306/Note.jpg" with substring jira/rally/images using regular expression in Java.
Below is the query to get the list of the String which contains substring src="/slm/attachment/63338424306/Note.jpg"
criteria.add(Restrictions.like("comment", "%<img%"));
criteria.setMaxResults(1);
List<Comments> list = criteria.list();
How can i replace using regex? Please help me here.
Let's say xxxxxxxxsrc="/slm/attachment/63338424306/Note.jpgxxxxxxxx is the string then after the replacement I am expecting xxxxxxxxsrc="jira/rally/images/Note.jpgxxxxxxxx
the no. 63338424306 can be any random no.
image name & format 'Note.jpg' can be changed i.e. 'abc.png' etc.
Basically, I want to replace /slm/attachment/63338424306/ with jira/rally/images
Thanks to all of you for your answers. I have updated the question little bit, please help me with that.
yourString.replaceAll("src=\"/slm/attachment", "src=\"/jira/rally/images");
You could use a capturing group for the src=" part and match the part that you want to replace.
(src\s*=\s*")/slm/attachment/\d+
( Capture group
src\s*=\s*" Match src, 0+ whitespace chars, =, 0+ whitespace chars and "
) Close group
/slm/attachment/ Match literally
\d+ Match 1+ digits
Note that if you want to match 0+ spaces only and no newlines, you could use a space only or [ \t]* to match a space and tab instead of \s*
In Java
String regex = "(src\\s*=\\s*\")/slm/attachment/\\d+";
And use the first capturing group in the replacement:
$1jira/rally/images
Result:
src="jira/rally/images/Note.jpg
Regex demo | Java demo
For example:
String string = "src = \"/slm/attachment/63338424306/Note.jpg";
System.out.println(string.replaceAll("(src\\s*=\\s*\")/slm/attachment/\\d+", "$1jira/rally/images"));
// src = "jira/rally/images/Note.jpg
You can use the following replacement sequences:
String a = "abc 123 src=\"/slm/attachment/63338424306/Note.jpg abc 132";
String b = "abc 123 src=\"/slm/attachment/61118424306/Note.jpg xyz";
String c = "123xxsrc=\"/slm/attachment/51238424306/Note.jpgxx324";
System.out.println(a.replaceAll("(?<=src=\")/slm/attachment/\\d+","jira/rally/images"));
System.out.println(b.replaceAll("(?<=src=\")/slm/attachment/\\d+","jira/rally/images"));
System.out.println(c.replaceAll("(?<=src=\")/slm/attachment/\\d+","jira/rally/images"));
output:
abc 123 src="jira/rally/images/Note.jpg abc 132
abc 123 src="jira/rally/images/Note.jpg xyz
123xxsrc="jira/rally/images/Note.jpgxx324
regex demo: https://regex101.com/r/ZtRg49/7/

How to replace last letter to another letter in java using regular expression

i have seen to replace "," to "." by using ".$"|",$", but this logic is not working with alphabets.
i need to replace last letter of a word to another letter for all word in string containing EXAMPLE_TEST using java
this is my code
Pattern replace = Pattern.compile("n$");//here got the real problem
matcher2 = replace.matcher(EXAMPLE_TEST);
EXAMPLE_TEST=matcher2.replaceAll("k");
i also tried "//n$" ,"\n$" etc
Please help me to get the solution
input text=>njan ayman
output text=> njak aymak
Instead of the end of string $ anchor, use a word boundary \b
String s = "njan ayman";
s = s.replaceAll("n\\b", "k");
System.out.println(s); //=> "njak aymak"
You can use lookahead and group matching:
String EXAMPLE_TEST = "njan ayman";
s = EXAMPLE_TEST.replaceAll("(n)(?=\\s|$)", "k");
System.out.println("s = " + s); // prints: s = njak aymak
Explanation:
(n) - the matched word character
(?=\\s|$) - which is followed by a space or at the end of the line (lookahead)
The above is only an example! if you want to switch every comma with a period the middle line should be changed to:
s = s.replaceAll("(,)(?=\\s|$)", "\\.");
Here's how I would set it up:
(?=.\b)\w
Which in Java would need to be escaped as following:
(?=.\\b)\\w
It translates to something like "a character (\w) after (?=) any single character (.) at the end of a word (\b)".
String s = "njan ayman aowkdwo wdonwan. wadawd,.. wadwdawd;";
s = s.replaceAll("(?=.\\b)\\w", "");
System.out.println(s); //nja ayma aowkdw wdonwa. wadaw,.. wadwdaw;
This removes the last character of all words, but leaves following non-alphanumeric characters. You can specify only specific characters to remove/replace by changing the . to something else.
However, the other answers are perfectly good and might achieve exactly what you are looking for.
if (word.endsWith("char oldletter")) {
name = name.substring(0, name.length() - 1 "char newletter");
}

Parse and remove special characters in java regex

So we were looking at some of the other regex posts and we are having trouble removing a special case in one instance; the special character is in the beginning of the word.
We have the following line in our code:
String k = s.replaceAll("([a-z]+)[()?:!.,;]*", "$1");
where s is a singular word. For example, when parsing the sentence "(hi hi hi)" by tokenizing it, and then performing the replaceAll function on each token, we get an output of:
(hi
hi
hi
What are we missing in our regex?
You can use an easier approach - replace the characters that you do not want with spaces:
String k = s.replaceAll("[()?:!.,;]+", " ");
Position matters so you would need to match the excluded charcters before the capturing group also:
String k = s.replaceAll("[()?:!.,;]*([a-z]+)[()?:!.,;]*", "$1");
your replace just removed the "special chars" after the [a-z]+, that's why the ( before hi is left there.
If you know s is a single word
you could either:
String k = s.replaceAll("\\W*(\\w+)\\W*", "$1");
or
String k = s.replaceAll("\\W*", "");
This can be more simple
try this :
String oldString = "Hi There ##$ What is %#your name?##$##$ 0123$$";
System.out.println(oldString.replaceAll("[\\p{Punct}\\s\\d]+", " ");
output :
Hi There What is your name 0123
So it also accepts numeric.
.replaceAll("[\p{Punct}\s\d]+", " ");
will replace alll the Punctuations used which includes almost all the special characters.

Java - Split by delimiters

Given _<A_>_<B_>_<Z_>, I want to extract A, B, C in an array.
Basically _< is the starting delimiter and _> is the ending delimiter.
You can use lookaround assertions to match only the content of the tags.
String text = "_<A_>_<B_>_<Z_>";
List<String> Result = new ArrayList<String>();
Pattern p = Pattern
.compile("(?<=_<)" + // Lookbehind assertion to ensure the opening tag before
".*?" + // Match a less as possible till the lookahead is true
"(?=_>)" // Lookahead assertion to ensure the closing tag ahead
);
Matcher m = p.matcher(text);
while(m.find()){
Result.add(m.group(0));
}
That's simple - cut out first opening and last closing , and then split it by close-open
string.replaceFirst( "^_<(.*)_>$", "$1" ).split( "_>_<" );
You extract them using capture groups.
split by _< to get 2 elements, take the 2nd and split it by _> to get 2 elements, take the 1st and split it by _>_< to get A, B, C

Categories