This question already has answers here:
Matching a space in regex
(10 answers)
Closed 3 years ago.
I'm using this regex to get just a number back.
Regex.Replace(foo, "[^.0-9]", "")
How do I make it not remove spaces?
That's easy:
Regex.Replace(foo, "[^.0-9\\s]", "")
You may find the Regex slightly easier to read with the #"" terminology (no need to escape the backslash:
Regex.Replace(foo, #"[^.0-9\s]", "")
How about something like this:
[^(.0-9)|( )]
Regex.Replace(yourString, "[^.0-9\\s]", "");
Related
This question already has answers here:
Efficient way to remove ALL whitespace from String?
(18 answers)
Closed 3 years ago.
I'm looking for a efficient way for removing all of the white spaces in an string.
I have checked replace (replace(' ','')) but I'm looking for a more efficient way.
I'd appreciate the help.
You may use Regular Expression.
For example:
var result=System.Text.RegularExpressions.Regex.Replace(input, #"\s+", "");
Input is your string
See more Removing whitespaces using C#
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I have a target string like:
"addr: line1
line2
tel:12345678"
note: between line2 and tel, there might be 1 or multiple new lines:\r\n or \r\n\r\n or more.
The result I want to get is as below:
"addr: line1
line2"
no \r\n under line2.
My questions are:
1)If I use
/addr[\s\S]+(?=(\r\n)+tel)/
, i will get the addr without tel, but I can't get rid of "\r\n"s under "line2", how could I do that?
2)I know [\s\S] represents any characters including \r,\n, and (.|\n|\r) can do that too.
But why [.\n] can't? It's just like the syntax of[\s\S] isn't it?
Thank you very much!
You need to make the first "+" non-greedy, so that it does not match the whole whitespace before "tel"
var regex = new Regex(#"addr.+?(?=\s+tel)", RegexOptions.Singleline);
var result = regex.Match(text).Value;
This question already has answers here:
C# Regex to allow only alpha numeric
(6 answers)
Closed 5 years ago.
I need to check whether string is only contain letters but not numbers or special characters.I used below regex pattern,
String validText = "^[a-zA-Z-]+$";
its work fine for 'Leo#' but if it is like 'Leo#1' its not working properly.
Anyone have idea ?
I prefer you can use LinQ (input is your test string)
bool result = input.All(Char.IsLetter);
Else as Gordon Posted the right Regex,
^[a-zA-z]+$
You can try using this regex
/^[A-Za-z]+$/
This will match only letters in your string ..
This question already has answers here:
How to use string.Endswith to test for multiple endings?
(9 answers)
Closed 5 years ago.
I need to check if a string last word is either "...abc" or "...xyz" or "...fgh".
How i can achieve the same thing using regex as i am trying to learn it?
e.g Sentence 1: Hi My Name is abc.
Sentence 2: I live in xyz.
The above sentence is a sample one to demonstrate.
You don't need any Regex. Just use String.EndsWith :
string a = "asdasd abc";
Console.WriteLine(a.EndsWith("abc.") || a.EndsWith("xyz.") || a.EndsWith("fgh."));
You can use this simple regex pattern:
(abc|xyz|fgh)$
Put your possible options between parenthesis separated by pipes. The $ means the end of the string.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
string regex = "<Name[.\\s]*>[.]*s[.]*</Name>";
string source = "<Name xmlns=\"http://xml.web.asdf.com\">Session</Name>";
bool hit = System.Text.RegularExpressions.Regex.IsMatch(
source,
regex,
System.Text.RegularExpressions.RegexOptions.IgnoreCase
);
Why is hit false? I'm trying to find any Name XML field that has an 's' in the name. I don't understand what could be wrong.
Thanks!
You are using . in a character class, where it means literally ., I think you mean to use in the sense of any character - so .* rather than [.]*
string regex = "<Name(.|\\s)*>.*s.*</Name>";
With XPath, this could be as easy as /Name[contains(.,'s')]