This question already has answers here:
How do I write a backslash (\) in a string?
(6 answers)
Why \b does not match word using .net regex
(2 answers)
Closed 3 years ago.
I have regex like this:
(?i)^(?!.*\bWITH\b).*\(\s*.*\s*\b(INDEX|FASTFIRSTROW|HOLDLOCK|SERIALIZABLE|REPEATABLEREAD|READCOMMITTED|READUNCOMMITTED|ROWLOCK|PAGLOCK|TABLOCK|TABLOCKX|NOLOCK|UPDLOCK|XLOCK|READPAST)\b\s*.*\s*\)
It return true in http://regexstorm.net.
But when i run in C#, it always return false.
String input to text:
INNER JOIN t_hat_meisaimidasi AS MM (READCOMMITTED, NOLOCK) WHERE ( AND hat_kanri_no = ?
Can someone explain me why?
Returns true for me; probably you didn't use #"...", so the escape tokens (\b etc) aren't what you think they are:
Console.WriteLine(Regex.IsMatch(
#"INNER JOIN t_hat_meisaimidasi AS MM (READCOMMITTED, NOLOCK) WHERE ( AND hat_kanri_no = ?",
#"(?i)^(?!.*\bWITH\b).*\(\s*.*\s*\b(INDEX|FASTFIRSTROW|HOLDLOCK|SERIALIZABLE|REPEATABLEREAD|READCOMMITTED|READUNCOMMITTED|ROWLOCK|PAGLOCK|TABLOCK|TABLOCKX|NOLOCK|UPDLOCK|XLOCK|READPAST)\b\s*.*\s*\)"));
Note: "\b" is a string of length 1 that contains a backspace character; #"\b" is a string of length 2 that contains a slash and a b. When dealing with regex, you almost always want to use a verbatim string literal (#"...").
To make it even better: Visual Studio will use colorization to tell you when you're getting it right:
Related
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:
Regular Expression Except this Characters
(4 answers)
C# Regular Expressions, string between single quotes
(4 answers)
Closed 5 years ago.
How do I match anything between single quotes? I need to match all attribute = 'some value' statements within a WHERE clause of queries. I tried:
= '(.+)'
But that doesn't work: somehow messes up all single quotes and matches.
If anyone could help me out it'd be much appreciated!
Try:
= '([^']*)'
Meaning you want anything/everything from = ' that isn't a single quote up to a single quote.
Python example:
import re
text = "attribute = 'some value'"
match = re.search("= '([^']*)'", text)
print(match.group(1))
To read more about that, it is called a negated character class: https://www.regular-expressions.info/charclass.html
This question already has answers here:
How do I remove diacritics (accents) from a string in .NET?
(22 answers)
C# equivalence of python maketrans and translate
(1 answer)
Closed 5 years ago.
I want to write a F# or C# function that removes Spanish accents from a string like so:
in: "a stríng withóut áccents"
->
out: "a string without accents"
I know how to achieve this in Python 3:
trans_table = str.maketrans( "áéíóúñÁÉÍÓÚÑàèìòùäëïöü", "aeiounAEIOUNaeiouaeiuo" )
# trans_table now contains a dictionary: { "á" : "a", "é" : "e", ... }
"A stríng withóut áccents".translate( trans_table )
# result is: A string without accents
Mimicking this solution would be rather straight forward if the characters to be translated where regular ascii characters (in the 0-127 range). However, because of the way .NET encodes strings it doesn't seem straight forward to do it for actual unicode characters out of this range...
I would like a solution that does not imply doing a regex-replace for each one of the many accented characters but that hopefully loops over the string only once...
Any ideas?
This question already has answers here:
Remove text in-between delimiters in a string (using a regex?)
(5 answers)
Closed 6 years ago.
I am trying to remove characters starting from (and including) rgm up to (and including) ;1..
Example input string:
Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})
Code:
string strEx = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
strEx = strEx.Substring(0, strEx.LastIndexOf("rgm")) +
strEx.Substring(strEx.LastIndexOf(";1.") + 3);
Result:
Sum ({rgmdaerub;1.Total_Value}, {.Major_Value})
Expected result:
Sum ({Total_Value}, {Major_Value})
Note: only rgm and ;1. will remain static and characters between them will vary.
I would recommend to use Regex for this purpose. Try this:
string input = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
string result = Regex.Replace(input, #"rgm.*?;1\.", "");
Explanation:
The second parameter of Regex.Replace takes the pattern that consists of the following:
rgm (your starting string)
. (dot - meaning any character)
*? (the preceding symbol can occure zero or more times, but stops at the first possible match (shortest))
;1. (your ending string - the dot needed to be escaped, otherwise it would mean any character)
You need to use RegEx, with an expression like "rgm(.);1\.". That's just off the top of my head, you will have to verify the exact regular expression that matches your pattern. Then, use RegEx.Replace() with it.