String s="Swamy Application";
s=s.replaceAll("\\S"," ");
system.out.println(s);
Should return String but we are getting empty
I need explanation What happening in \\S.
String.replaceAll() takes a regex as first parameter and the replacement text for all matches of that regex as second parameter. Here, you have given \\S as the first parameter, which matches every non-whitespace character. The replacement string given is a whitespace. So the returned String would be having whitespaces only.
\S matches any non-whitespace character which is leading to replace the alpha characters in the string to whitespace.
"Swamy Application" -> " "
More about this at source
If you are trying to replace the whitespace character from the string then use:
"\s"
else if you are trying to replace the only S character from the string then use:
"S"
Related
I have a string like,
string basestring= "A\B+C+E\FG+\K987+54h\";
I have to replace all the occurrence of special character with the same special character and .
Output should be :
"A\\B\+C\+E\\FG\+\\K987\+54h\\"
Currently I am using the following code to get the result.
regex Reg("[^A-Z0-9]", regex_constants::icase);
string help = regex_replace("A\B+C+E\FG+\K987+54h\", Reg, "\\");
This will replace all the special characters with \, How to get the last occurred special character ?
Use
std::regex Reg("[^A-Z0-9]", std::regex_constants::icase);
std::string help = std::regex_replace(R"(A\B+C+E\FG+\K987+54h\)", Reg, R"(\$&)");
The replacement is a backslash followed with the match value (the backreference to the whole match is $&).
When you do this replacement:
regex_replace("A\B+C+E\FG+\K987+54h\", Reg, "\\");
you are substituting \\ exactly for every pattern you match.
To get the desired output, you need to substitute in the matched pattern along with the escape characters, like this:
std::regex_replace(R"(A\B+C+E\FG+\K987+54h\)", Reg, "\\$0");
// insert \\ before the match ^^^^
Also note that the input string needs to escape the special characters, or be written as a raw-string literal.
Here's a demo.
String val = "this is a string *"
I am attempting to remove the final character '*' along with any preceding whitespace with the following output:
String result = "this is a string"
What's the simplest and sturdiest approach to doing this?
Should I substring the final character and trim the string or use regex?
For matching you can use:
\s*\*$
Which is 0 or more whitespace characters followed by literal * character with anchor $ to assert end of line.
That needs to be replaced by empty string "" to remove it.
RegEx Demo
I am looking for a regular expression to remove all special characters from a string, except whitespace. And maybe replace all multi- whitespaces with a single whitespace.
For example "[one# !two three-four]" should become "one two three-four"
I tried using str = Regex.Replace(strTemp, "^[-_,A-Za-z0-9]$", "").Trim() but it does not work. I also tried few more but they either get rid of the whitespace or do not replace all the special characters.
[ ](?=[ ])|[^-_,A-Za-z0-9 ]+
Try this.See demo.Replace by empty string.See demo.
http://regex101.com/r/lZ5mN8/69
Use the regex [^\w\s] to remove all special characters other than words and white spaces, then replace:
Regex.Replace("[one# !two three-four]", "[^\w\s]", "").Replace(" ", " ").Trim
METHOD:
instead of trying to use replace use replaceAll eg :
String InputString= "[one# !two three-four]";
String testOutput = InputString.replaceAll("[\\[\\-!,*)##%(&$_?.^\\]]", "").replaceAll("( )+", " ");
Log.d("THE OUTPUT", testOutput);
This will give an output of one two three-four.
EXPLANATION:
.replaceAll("[\\[\\-!,*)##%(&$_?.^\\]]", "") this replaces ALL the special characters present between the first and last brackets[]
.replaceAll("( )+", " ") this replaces more than 1 whitespace with just 1 whitespace
REPLACING THE - symbol:
just add the symbol to the regex like this .replaceAll("[\\[\\-!,*)##%(&$_?.^\\]]", "")
Hope this helps :)
String litertal consist zero or more character enclosed by double quote(").
Use escape sequences(listed below) to represent special characters within a string.
It is a compile-time error for a newline or EOF characterto appear inside a string literal.
All the supported escape sequences are as follow:
\b backspace
\f formfeed
\r carriage return
\n newline
\t tab
\" double quote
\ backslash
The following are valid examples of string literal:
" This is a string contain tab \t"
" Hello stackoverflow \"\b"
Can you help me write a regex match string literal?
Thanks so much.
The most general way is to use Pattern.quote() method which returns a regular expression that matches the literal string passed as its argument. You can use it in Scala as well as in Java.
If you want to match e.g. the string represented by the literal "contain tab \t", you would use the regexp "contain tab \t".r—so, there is no need for any special handling of TAB inside the regexp.
Lets say I have string like this,
String sample = "This is a sample string with more than two spaces in a string ";
Now what I have to do to make the string to have only one space between each word.
Thanks in advance.
If literally mean "space" then you can replace / {2,}/ with a single space. Note that a space in the regular expression matches a space in the text.
If by "space" you actually mean "all whitespace" (spaces, tabs, newlines, etc.), then use \s+ instead.
Match them with \s+ regex and replace with single space..
If you only want to replace multiple spaces (not tabs and other white chars) use \ +
If this is C#, then you can do
using System.Text.RegularExpressions;
…
String sample = "This is a sample string with more than two spaces in a string ";
sample = Regex.Replace(sample, #"\s+", " ");