I have this String:
String str = "<p>23.5</p>";
And i want to replace the dot for comma only inside elements. The output i need is:
<p>23,5</p>
I cant figure it out, i have this:
str = str.replaceAll("(?<=<p>)\\.(?=</p>)", ",");
But it doesnt work. I need to replace dot only in elements with particular tag (is an xml in a String), in this case .
Thank you
You may use capturing groups + escape the /:
str = str.replaceAll("(?<=<p>)(\\d*)\\.(\\d+)(?=<\\/p>)", "$1,$2");
If you want to replace dot in all numbers, you may just as well use
str = str.replaceAll("(\\d*)\\.(\\d+)", "$1,$2");
Following regex will match the dot character that is between numerical characters
(?<=\d)\.(?=\d)
Regex Explanation:
\d - match any digit between 0-9
(?<=\d)\. - positive look-behind to match any . character that has a digit just before it
\.(?=\d) - positive look-ahead to match any . character that has a digit just after it
Demo:
https://regex101.com/r/WMEjPl/1
Java Code Example:
public static void main(String args[]) {
String regex = "(?<=\\d)\\.(?=\\d)";
String str = "<p>23.5</p>";
String str2 = "Mr. John <p>23.5</p> Hello";
String str3 = "Mr. John <p>23.5</p> Hello 12.2324";
System.out.println(str.replaceAll(regex, ",")); // <p>23,5</p>
System.out.println(str2.replaceAll(regex, ",")); // Mr. John <p>23,5</p> Hello
System.out.println(str3.replaceAll(regex, ",")); // Mr. John <p>23,5</p> Hello 12,2324
}
Related
I am dealing with some cells where I have to extract certain letters from these cells. I want to replace a whole String with " " except from one single-standing character. The biggest challenge to me has been to tell my Regex code only to look for a single Char and remove everything else.
To further elaborate and simplify; I need my Regex to replace everything with "" execept from a single character that standa ALONE (I.E white spaces left and right or linked to a number)
public class Main {
public static void main(String[] args) {
String test = "22A 302 abc";
String works = test.replaceAll("^\\w[\\s\\S]*", " ");
System.out.println(works);
//Desired result: A
}
}
You could match a digit or space before and after capturing a char [A-Z].
In the replacement use group 1.
^.*[\d ]([A-Z])[ \d].*$
Regex demo | Java demo
If there can be only a single uppercase char in the string:
^[^A-Z]*[\d ]([A-Z])[ \d][^A-Z]*$
Regex demo
Example code
String test = "22A 302 abc";
String works = test.replaceAll("^.*[\\d ]([A-Z])[ \\d].*$", "$1");
System.out.println(works);
Output
A
To match between digits 0-9, horizontal whitespace chars or punctuations:
String works = test.replaceAll("^.*[\\p{P}0-9\\h]([A-Z])[\\p{P}0-9\\h].*$", "$1");
You can do it as follows:
public class Test {
public static void main(String[] args) {
String test = "22A 302 abc";
String works = test.replaceAll("\\d+([A-Z]).*", "$1");
System.out.println(works);
}
}
Output:
A
Explanation: Replace everything with capturing group-1 ($1 in the code given above) which has just a letter A-Z preceded by an integer number(\\d+) and can have anything (.*) after it.
I am looking for a RegEx (Java) which will append '~' char after end of each word.
My requirement is:
Append ~ at the end of each word
If word has any special char in it, then do not append '~'.
If there are multiple whitespaces, it should be trim to single whitespace.
Please have a look on my example below :
Input: Hello World How* A1e Y?u
Output: Hello~ World~ How* A1e~ Y?u
I took help from forum and could achieve it but I am not able to achieve #2.
My code snippet:
pattern = ([^\\s][a-zA-Z0-9])(\\s|$);
pattern.matcher(searchTerm).replaceAll("$1~$2");
How can I skip append operation if word has any special char?
Please suggest.
I suggest using
searchTerm = searchTerm.replaceAll("(?<!\\S)\\w++(?!\\S)", "$0~").replaceAll("\\s{2,}", " ").trim();
See the Java demo
Details
(?<!\S) - a negative lookbehind making sure there is either a whitespace or start of string right before the current location
\w++ - 1 or more word chars
(?!\S) - a negative lookahead making sure there is either a whitespace or start of string right after the current location.
The $0 is the whole match value.
The .replaceAll("\\s{2,}", " ") (for regular spaces, just replace \\s with a space) part "shrinks" any two or more whitespace characters to a single space, and .trim() part trims the result from whitespace on both ends.
This might help:
public static void main(String[] args) {
String input = "Hello World How* A1e Y?u word";
String extraSpaceInput = String.format(" %s ", input.replaceAll("\\s+", " "));
// Wanted output: Output: Hello~ World~ How* A1e~ Y?u word
Pattern pattern = Pattern.compile("\\s([a-zA-Z0-9]+)\\s");
String output = pattern.matcher(extraSpaceInput).replaceAll("$1~ ");
String cleanedUpOutput = output.replaceAll("\\s+", " ").trim();
// My output: "Hello~ World~ How* A1e~ Y?u word~"
System.out.println("My output: \"" + cleanedUpOutput + "\"");
}
I used a regex expression to remove special characters from name. The expression will remove all letters except English alphabets.
public static void main(String args[]) {
String name = "Özcan Sevim.";
name = name.replaceAll("[^a-zA-Z\\s]", " ").trim();
System.out.println(name);
}
Output:
zcan Sevim
Expected Output:
Özcan Sevim
I get bad result as I did it this way, the right way will be to remove special characters based on ASCII codes so that other letters will not be removed, can someone help me with a regex that would remove only special characters.
You can use \p{IsLatin} or \p{IsAlphabetic}
name = name.replaceAll("[^\\p{IsLatin}]", " ").trim();
Or to remove the punctuation just use \p{Punct} like this :
name = name.replaceAll("\\p{Punct}", " ").trim();
Outputs
Özcan Sevim
take a look at the full list of Summary of regular-expression constructs and use the one which can help you.
Use Guava CharMatcher for that :) It will be easier to read and maintain it.
name = CharMatcher.ASCII.negate().removeFrom(name);
use [\W+] or "[^a-zA-Z0-9]" as regex to match any special characters and also use String.replaceAll(regex, String) to replace the spl charecter with an empty string. remember as the first arg of String.replaceAll is a regex you have to escape it with a backslash to treat em as a literal charcter.
String string= "hjdg$h&jk8^i0ssh6";
Pattern pt = Pattern.compile("[^a-zA-Z0-9]");
Matcher match= pt.matcher(string);
while(match.find())
{
String s= match.group();
string=string.replaceAll("\\"+s, "");
}
System.out.println(string);
I have a string which I want to first split by space, and then separate the words from the special characters.
For Example, let's say the input is:
Hi, How are you???
I already wrote the logic to split by space here:
String input = "Hi, How are you???";
String[] words = input.split("\\\\s+");
Now, I want to seperate each word from the special character.
For example: "Hi," to {"Hi", ","} and "you???" to {"you", "???"}
If the string does not end with any special characters, just ignore it.
Can you please help me with the regular expression and code for this in Java?
Following regex should help you out:
(\s+|[^A-Za-z0-9]+)
This is not a java regex, so you need to add a backspace.
It matches on whitespaces \s+ and on strings of characters consisting not of A-Za-z0-9. This is a workaround, since there isn't (or at least I do not know of) a regex for special characters.
You can test this regex here.
If you use this regex with the split function, it will return the words. Not the special characters and whitespaces it machted on.
UPDATE
According to this answer here on SO, java has\P{Alpha}+, which matches any non-alphabetic character. So you could try:
(\s|\P{Alpha})+
I want to separate each word from the special character.
For example: "Hi," to {"Hi", ","} and "you???" to {"you", "???"}
regex to achieve above behavior
String stringToSearch ="Hi, you???";
Pattern p1 = Pattern.compile("[a-z]{0}\\b");
String[] str = p1.split(stringToSearch);
System.out.println(Arrays.asList(str));
output:
[Hi, , , you, ???]
#mike is right...we need to split the sentence on special characters, leaving out the words. Here is the code:
`public static void main(String[] args) {
String match = "Hi, How are you???";
String[] words = match.split("\\P{Alpha}+");
for(String word: words) {
System.out.print(word + " ");
}
}`
I'm trying to use the String.replaceAll() method with regex to only keep letter characters and ['-_]. I'm trying to do this by replacing every character that is neither a letter nor one of the characters above by an empty string.
So far I have tried something like this (in different variations) which correctly keeps letters but replaces the special characters I want to keep:
current = current.replaceAll("(?=\\P{L})(?=[^\\'-_])", "");
Make it simplier :
current = current.replaceAll("[^a-zA-Z'_-]", "");
Explanation :
Match any char not in a to z, A to Z, ', _, - and replaceAll() method will replace any matched char with nothing.
Tested input : "a_zE'R-z4r#m"
Output : a_zE'R-zrm
You don't need lookahead, just use negated regex:
current = current.replaceAll("[^\\p{L}'_-]+", "");
[^\\p{L}'_-] will match anything that is not a letter (unicode) or single quote or underscore or hyphen.
Your regex is too complicated. Just specify the characters you want to keep, and use ^ to negate, so [^a-z'_-] means "anything but these".
public class Replacer {
public static void main(String[] args) {
System.out.println("with 1234 &*()) -/.,>>?chars".replaceAll("[^\\w'_-]", ""));
}
}
You can try this:
String str = "Se#rbi323a`and_Eur$ope#-t42he-[A%merica]";
str = str.replaceAll("[\\d+\\p{Punct}&&[^-'_\\[\\]]]+", "");
System.out.println("str = " + str);
And it is the result:
str = Serbia'and_Europe-the-[America]