I am trying to use regex.replace to strip out unwanted characters, but I need to account for spaces:
string asdf = "doésn't work?";
string regie = #"([{}\(\)\^$&._%#!#=<>:;,~`'\’ \*\?\/\+\|\[\\\\]|\]|\-)";
Response.Write(Regex.Replace(asdf,regie,"").Replace(" ","-"));
returns doésntwork instead of doésnt-work
Ideas?
Thanks!
Your regular expression includes a space, so the space gets stripped out before the string.Replace is called.
string regie = #"([{}\(\)\^$&._%#!#=<>:;,~`'\’ \*\?\/\+\|\[\\\\]|\]|\-)";
^ here
Remove it from the regular expression and your code should do what you expect:
string regie = #"([{}\(\)\^$&._%#!#=<>:;,~`'\’\*\?\/\+\|\[\\\\]|\]|\-)";
You have a space inside your regex, right here: \’ \*.
Related
I need the regular expression for below string cases,
String value = "�江苏银行股份有限公司南京迈皋桥支行";
String value = "�/CNYXB/02112";
in both the cases only the character "�" needs to be removed and the final string values should be as below after applying regular expression,
String value = "江苏银行股份有限公司南京迈皋桥支行";
String value = "/CNYXB/02112";
thanks in advance!!!
yes i have tried below regEx,
value = value.replaceAll("[^\\p{ASCII}]", "");
I'm not sure if this is what you're actually asking, but you can easily remove the first character from the string:
^.
matches the first character at the start of the string.
If you want to remove an out-of-range character then you need to define your range. Use multiple classes wiht octal escapes, so something like:
[\o{2444}-\o{3444}\o{40}-\o{77}]
without know what the characters you're looking for really are it's difficult to be more specific.
try to use replaceFirst instead of replaceAll:
value = value.replaceFirst("[^\\p{ASCII}]", "");
I want to write a regex that will match any time the substring "my-app" is encountered inside any given string.
I have the following Groovy code:
String regex = ".*my-app*"
String str = getStringFromUserInput()
if(str.matches(regex) {
println "Match!"
} else {
println "Doesn't match..."
}
When getStringFromUserInput() returns a string like "blahmy-appfizz", the code above still reports Doesn't match.... So I figured that hyphens must be a special character in regexes and tried changing the regex to:
String regex = ".*my--app*"
But still nothing has changed. Any ideas as to where I'm going wrong?
The hyphen is no special character.
matches validates the entire input. Try:
String regex = ".*my-app.*"
Note that p* matches zero or more p's and p.* matches a p followed by zero or more chars (other than line breaks).
Assuming getStringFromUserInput() does not leave any line break char in the input. In which case you'd need to do a trim() to get rid of it, since the .* does not match line break chars.
String.contains seems like a simpler solution than a regex, e.g.
String stringFromUser = 'my-app'
assert 'foomy-appfoo'.contains(stringFromUser)
assert !'foo'.contains(stringFromUser)
I want a regix format that Must be alphabets and special characters (like space, ‘, -) but numeric value should not be taken.
I tried with this expression /^[a-zA-Z ]*$/ but it treats space as special character.
Please Help.
/^[a-zA-Z\s\-\'\"]*$/
use this.
This will contain any alphabet([upper/lower]case)
,space,
hiphen,
",
'
update
If you are using it inside NSPredicate
then make sure that you put the - in the end, as it throws error.
Move it to the end of the sequence to be the last character before the closing square bracket ].
like this [a-zA-Z '"-]
If you want only the alphabets and space, ' and - then:
/^[-a-zA-Z\s\']+$/
Notice the + from above instead of *. If you use * then it will match with empty string, where the + sign means to have at least one character in your input.
Now, if you want to match any alphabets with any special characters(not only those three which are mentioned), then I'll just you to use this one:
/^\D+$/
It means any characters other than digits!
Maybe try this:
\b[a-zA-Z \-\']+\b
http://regex101.com/r/oQ5nU9
You can use it defiantly work it
[a-zA-Z._^%$#!~#,-]+
this code work fine you can try it....
//Use this for allowing space as we all as other special character.
#"[a-zA-Z\\s\\-\\'\\"]"
//Following link will be help for further.
http://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet
Thanks for your response.. I finally resolved it with this
NSString characterRegex = #"^(\s[a-zA-Z]+(([\'\-\+\s]\s*[a-zA-Z])?[a-zA-Z])\s)+$";
NSPredicate *characterTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",characterRegex];
return [characterTest evaluateWithObject:inputString];
I'm very new to regular expression. I want to extract the following string
"109_Admin_RegistrationResponse_20130103.txt"
from this file content, the contents is selected per line:
01-10-13 10:44AM 47 107_Admin_RegistrationDetail_20130111.txt
01-10-13 10:40AM 11 107_Admin_RegistrationResponse_20130111.txt
The regular expression should not pick the second line, only the first line should return a true.
Your Regex has a lot of different mistakes...
Your line does not start with your required filename but you put an ^ there
missing + in your character group [a-zA-Z], hence only able to match a single character
does not include _ in your character group, hence it won't match Admin_RegistrationResponse
missing \ and d{2} would match dd only.
As per M42's answer (which I left out), you also need to escape your dot . too, or it would match 123_abc_12345678atxt too (notice the a before txt)
Your regex should be
\d+_[a-zA-Z_]+_\d{4}\d{2}\d{2}\.txt$
which can be simplified as
\d+_[a-zA-Z_]+_\d{8}\.txt$
as \d{2}\d{2} really look redundant -- unless you want to do with capturing groups, then you would do:
\d+_[a-zA-Z_]+_(\d{4})(\d{2})(\d{2})\.txt$
Remove the anchors and escape the dot:
\d+[a-zA-Z_]+\d{8}\.txt
I'm a newbie in php but i think you can use explode() function in php or any equivalent in your language.
$string = "01-09-13 10:17AM 11 109_Admin_RegistrationResponse_20130103.txt";
$pieces = explode("_", $string);
$stringout = "";
foreach($i = 0;$i<count($pieces);i++){
$stringout = $stringout.$pieces[$i];
}
How do I use regex to convert
11111aA$xx1111xxdj$%%`
to
aA$xx1111xxdj$%%
So, in other words, I want to remove (or match) the FIRST grouping of 1's.
Depending on the language, you should have a way to replace a string by regex. In Java, you can do it like this:
String s = "11111aA$xx1111xxdj$%%";
String res = s.replaceAll("^1+", "");
The ^ "anchor" indicates that the beginning of the input must be matched. The 1+ means a sequence of one or more 1 characters.
Here is a link to ideone with this running program.
The same program in C#:
var rx = new Regex("^1+");
var s = "11111aA$xx1111xxdj$%%";
var res = rx.Replace(s, "");
Console.WriteLine(res);
(link to ideone)
In general, if you would like to make a match of anything only at the beginning of a string, add a ^ prefix to your expression; similarly, adding a $ at the end makes the match accept only strings at the end of your input.
If this is the beginning, you can use this:
^[1]*
As far as replacing, it depends on the language. In powershell, I would do this:
[regex]::Replace("11111aA$xx1111xxdj$%%","^[1]*","")
This will return:
aA$xx1111xxdj$%%
If you only want to replace consecutive "1"s at the beginning of the string, replace the following with an empty string:
^1+
If the consecutive "1"s won't necessarily be the first characters in the string (but you still only want to replace one group), replace the following with the contents of the first capture group (usually \1 or $1):
1+(.*)
Note that this is only necessary if you only have a "replace all" capability available to you, but most regex implementations also provide a way to replace only one instance of a match, in which case you could just replace 1+ with an empty string.
I'm not sure but you can try this
[^1](\w*\d*\W)* - match all as a single group except starting "1"(n) symbols
In Javascript
var str = '11111aA$xx1111xxdj$%%';
var patt = /^1+/g;
str = str.replace(patt,"");