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
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.
I have this string example:
var s = 'type=audio&hls=&mp3=foo';
I would like to find everything between = and & and replace with quotes + matched value so I get this:
type="audio" hls="" mp3="foo"
(match is in quotes even if its empty and & gets replaced with space)
This is my regex but its not working:
s = s.replace(/=.+?\\&/g,function(a,inside){
return '="'+inside+'" ';
})
If we consider your regex one token at a time, here's what it means:
= matches a literal equal sign
.+? matches an optional string that has at least one character in it
\\ matches a literal backslash
& matches a literal ampersand
Among other problems, this requires that a literal backslash to be in the input string, and requires the string to end with an ampersand. Also .+? can be shortened to .*, but is still wrong because it might include ampersands and equal signs in the matched string.
Also, there is no need to replace with a function, as JavaScript can do what you are doing with just a string replacement.
A better regex might have these tokens:
= matches a literal equal sign
[^&]* matches a string (possibly empty) that does not contain ampersands
&? matches an optional ampersand
As Wiktor pointed out above, this could all be combined together like this:
s = s.replace(/=([^&]*)&?/g, '="$1" ').trim();
Here parentheses are used to mark the portion of the matched pattern that is being replaced, the $1 is used to refer to the marked portion of the pattern in parentheses, and the .trim() removes the trailing space.
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"
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.