I'd do:
- Find:
@(\w+)@ - Replace:
${$1}
Check Regular expression.
Explanation:
@ : literally @
( : start group 1
\w+ : 1 or more word character
) : end group 1
@ : literally @
Answer from Toto on Stack ExchangeYou could use a regular expression and Dictionary<string, string> to do the search replace:
// This regex matches either one of the special characters, or a sequence of
// more than one whitespace characters.
Regex regex = new Regex("['\"&,\\\\]|\\s{2,}");
var map = new Dictionary<string, string> {
{ "'", "`"},
{ "\"", "`"},
{ "&", "and" },
{ ",", ":" },
{ "\\", "/" }
};
// If the length of the match is greater that 1, then it's a sequence
// of spaces, and we can replace it by a single space. Otherwise, we
// use the dictionary to map the character.
string output = regex.Replace(input.ToUpper(),
m => m.Value.Length > 1 ? " " : map[m.Value]);
When I had to do something similar, I used a Dictionary<string, string> to define the replacements.
And then something like this to replace:
foreach( KeyValuePair<string, string> pair in replacements)
{
str = str.Replace(pair.Key, pair.Value);
}
http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx
That depends on what you mean. If you just want to get rid of them, do this:
(Update: Apparently you want to keep digits as well, use the second lines in that case)
String alphaOnly = input.replaceAll("[^a-zA-Z]+","");
String alphaAndDigits = input.replaceAll("[^a-zA-Z0-9]+","");
or the equivalent:
String alphaOnly = input.replaceAll("[^\\p{Alpha}]+","");
String alphaAndDigits = input.replaceAll("[^\\p{Alpha}\\p{Digit}]+","");
(All of these can be significantly improved by precompiling the regex pattern and storing it in a constant)
Or, with Guava:
private static final CharMatcher ALNUM =
CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z'))
.or(CharMatcher.inRange('0', '9')).precomputed();
// ...
String alphaAndDigits = ALNUM.retainFrom(input);
But if you want to turn accented characters into something sensible that's still ascii, look at these questions:
- Converting Java String to ASCII
- Java change áéőűú to aeouu
- ń ǹ ň ñ ṅ ņ ṇ ṋ ṉ ̈ ɲ ƞ ᶇ ɳ ȵ --> n or Remove diacritical marks from unicode chars
I am using this.
s = s.replaceAll("\\W", "");
It replace all special characters from string.
Here
\w : A word character, short for [a-zA-Z_0-9]
\W : A non-word character
preg_replace('/([0-9])-([0-9])/', '$1.$2', $string);
Should do the trick :)
Edit: some more explanation:
By using ( and ) in a regular expression, you create a group. That group can be used in the replacement. $1 get replaced with the first matched group, $2 gets replaced with the second matched group and so on.
That means that if you would (just for example) change '$1.$2' to '$2.$1', the operation would swap the two numbers.
That isn't useful behavior in your case, but perhaps it helps you understand the principle better.
Depending on the regex implementation you're using, you can use non-capturing groups:
preg_replace('/(?<=[0-9])-(?=[0-9])/', '.', $string);
In your case there are a couple of easy solutions. This first solution uses only basic regex:
[\.\w]+$
This captures every word character \w or period \. from the end $, stopping when it reaches any other type of character. This works because the space is not a word character or a period.
If you would like to select the region you want to remove, you can just use:
.*
Read this as match every character .* until the last space . The reason this matches until the last space, is because the star is a greedy quantifier. This means it matches everything it can, then starts giving back characters as needed for the rest of the pattern to match. To match until the first space, use .*? . The question mark next to the star makes the star lazy. This means the star will match as little as it has to in order for the next part of the expression to find a match.
However, capture groups are ideal for problems like this, where you would like to remove part of a line and keep the rest. A simple formula for this is:
^(.*)pattern_to_remove(.*)$
You can then recover the rest using backreferences. These store the capture groups (anything in parenthesis) from you regex into preset variables. In the pattern above, this would be \1\2 or $1$2 depending on the language you are using. This brings me to probably the easiest answer to your problem:
(.*)
The first capture group contains everything after the space. This regex is very straightforward and easy to read. Move to the first space , then capture everything after (.*).
For a comprehensive reference on regex, check out regular-expressions.info. Here is a link to the page on capture groups.
- Ctrl+H
- Find what:
^\S+\h+ - Replace with:
LEAVE EMPTY - CHECK Wrap around
- CHECK Regular expression
- Replace all
Explanation:
^ # beginning of line
\S+ # 1 or more non spaces
\h+ # 1 or more horizontal spaces
Screenshot (before):

Screenshot (after):
