Replace multiple whitespace in string with multiple special char in Java - java

String str = "hello there, what are you doing";
System.out.println(str.replaceAll("\\s{2,}", "?"));
output: hello?there what are?you doing
expected output: hello??there what are???you doing

Try this.
Pattern pat = Pattern.compile("\\s{2,}");
String str = "hello there, what are you doing";
System.out.println(pat.matcher(str).replaceAll(m -> "?".repeat(m.group().length())));
output:
hello??there, what are???you doing

A one line regex which uses replaces whitespace using look ahead (?=\s) and look behind (?<=\s) would be:
String str = "hello there, what are you doing here";
System.out.println(str.replaceAll("(\s(?=\s)|(?<=\s)\s)", "?"));
=>
hello??there, what are???you doing??????here
So the above matches whitespace followed by whitespace , or whitespace preceeded by whitespace and replaces this with "?".

Related

Java Regex ReplaceAll with grouping

I want to surround all tokens in a text with tags in the following manner:
Input: " abc fg asd "
Output:" <token>abc</token> <token>fg</token> <token>asd</token> "
This is the code I tried so far:
String regex = "(\\s)([a-zA-Z]+)(\\s)";
String text = " abc fg asd ";
text = text.replaceAll(regex, "$1<token>$2</token>$3");
System.out.println(text);
Output:" <token>abc</token> fg <token>asd</token> "
Note: for simplicity we can assume that the input starts and ends with whitespaces
Use lookaround:
String regex = "(?<=\\s)([a-zA-Z]+)(?=\\s)";
...
text = text.replaceAll(regex, "<token>$1</token>");
If your tokens are only defined with a character class you don't need to describe what characters are around. So this should suffice since the regex engine walks from left to right and since the quantifier is greedy:
String regex = "[a-zA-Z]+";
text = text.replaceAll(regex, "<token>$0</token>");
// meaning not a space, 1+ times
String result = input.replaceAll("([^\\s]+)", "<token>$1</token>");
this matches everything that isn't a space. Prolly the best fit for what you need. Also it's greedy meaning it will never leave out a character that it shouldn't ( it will never find the string "as" in the string "asd" when there is another character with which it matches)

Java pattern matching using regex

I am new to java coding and using pattern matching.I am reading this string from file. So, this will give compilation error. I have a string as follows :
String str = "find(\"128.210.16.48\",\"Hello Everyone\")" ; // no compile error
I want to extract "128.210.16.48" value and "Hello Everyone" from above string. This values are not constant.
can you please give me some suggestions?
Thanks
I suggest you to use String#split() method but still if you are looking for regex pattern then try it and get the matched group from index 1.
("[^"][\d\.]+"|"[^)]*+)
Online demo
Sample code:
String str = "find(\"128.210.16.48\",\"Hello Everyone\")";
String regex = "(\"[^\"][\\d\\.]+\"|\"[^)]*+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
output:
"128.210.16.48"
"Hello Everyone"
Pattern explanation:
( group and capture to \1:
" '"'
[^"] any character except: '"'
[\d\.]+ any character of: digits (0-9), '\.' (1
or more times (matching the most amount
possible))
" '"'
| OR
" '"'
[^)]* any character except: ')' (0 or more
times (matching the most amount
possible))
) end of \1
Try with String.split()
String str = "find(\"128.210.16.48\",\"Hello Everyone\")" ;
System.out.println(str.split(",")[0].split("\"")[1]);
System.out.println(str.split(",")[1].split("\"")[1]);
Output:
128.210.16.48
Hello Everyone
Edit:
Explanation:
For the first string split it by comma (,). From that array choose the first string as str.split(",")[0] split the string again with doublequote (") as split("\"")[1] and choose the second element from the array. Same the second string is also done.
The accepted answer is fine, but if for some reason you wanted to still use regex (or whoever finds this question) instead of String.split here's something:
String str = "find(\"128.210.16.48\",\"Hello Everyone\")" ; // no compile error
String regex1 = "\".+?\"";
Pattern pattern1 = Pattern.compile(regex1);
Matcher matcher1 = pattern1.matcher(str);
while (matcher1.find()){
System.out.println("Matcher 1 found (trimmed): " + matcher1.group().replace("\"",""));
}
Output:
Matcher 1 found (trimmed): 128.210.16.48
Matcher 1 found (trimmed): Hello Everyone
Note: this will only work if " is only used as a separator character. See Braj's demo as an example from the comments here.

Removing leading NewLine chars from String by regex?

I am new to "REGEX".And this question is training and educational for regex in java.
I try to remove leading new line chars from a string.(I know we can do this by 'trim()',but I do not want use it).I use '^[\r\n]+' as regex,like this:
str = "\r\n\r\ntest";
str.replaceAll("^[\r\n]+", "");
System.out.print(str);
I guess result will be
test
But the result is:
CRLF
CRLF
test
As you can see,leading new line chars do not removed.Also I try '^\s' or '^\s+' as regex,but result was same.Do you know why those regexes do not match leading new line chars?
A String cannot be modified: replaceAll returns the changed string.
str = str.replaceAll(...);

How to remove some word in a sentences Java?

Assume the string is:
The/at Fulton/np-tl County/nn-tl Grand/jj-tl
How can I remove character after / and the out put as below
The Fulton County Grand
It looks like a simple regex-based replace could work fine here:
text = text.replaceAll("/\\S*", "");
Here the \\S* means "0 or more non-whitespace characters". There are other options you could use too, of course.
String input = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String clean = input.replaceAll("/.*?(?= |$)", "");
Here's a test:
public static void main( String[] args ) {
String input = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String clean = input.replaceAll("/.*?(?= |$)", "");
System.out.println( clean);
}
Output:
The Fulton County Grand
String text = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String newText = text.replaceAll("/.*?\\S*", "");
From Java API:
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
String replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement.
If you need to replace a substring or a character, use 1st 2 methods.
If you need to replace a pattern or a regex, used 2nd 2 methods.
This worked for me:
String text = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String newText = text.replaceAll("/.*?(\\s|$)", " ").trim();
Yields:
The Fulton County Grand
This basically replaces any character(s) which are after a / and are either followed by a white space or else, by the end of the string. The trim() at the end is to cater for the extra white space added by the replaceAll method.
do as follow:
startchar : is a starting character from which you want to replace.
endchar : is a ending character up to chich character you want to replace.
" " : is because you just want to delete it so replace with white space
string.replaceAll(startchar+".*"+endchar, "")
refer http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29
also see greedy quantifier examples
see working example
public static void main( String[] args ) {
String startchar ="/";
String endchar="?(\\s|$)";
String input = "The/at Fulton/np-tl County/nn-tl Grand/jj-tl";
String clean = input.replaceAll(startchar+".*"+endchar, " ");
System.out.println( clean);
}
output
The Fulton County Grand

how to check if a space is followed by a certain character?

I am really confused on this regex things. I have tried to understand it, went no where.
Basically, i am trying to replace all spaces followed by every character but a space to be replaced with "PM".
" sd"
" sd"
however
" sd"
" sd"
This will replace the space and the following character with "PM":
String s = "123 axy cq23 dasd"; //your string
String newString = s.replaceAll(" [^ ]","PM");
Since I'm not sure if you want to replace only the space or the space and the following character, too, here is a slightly modified version that replaces only the space:
String s = "123 axy cq23 dasd"; //your string
String newString = s.replaceAll(" ([^ ])", "PM$1")
You need to use non-capturing pattern:
String res = oldString.replaceAll(" (?:[^ ])", "PM");

Categories

Resources