Regex Pattern to check only letters [duplicate] - c#

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 ..

Related

Regex return always false in C# [duplicate]

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:

C# Regex - match a substring in a filename [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Given a filesystem path, is there a shorter way to extract the filename without its extension?
(10 answers)
Closed 4 years ago.
My regex is really poor so I need help with a c# regex expression that can match a substring after the last backslash.
Typical input:
D:\DataFiles\Files_81\aars2016FAKH1800010.pdf
I need to check if the filename aars2016FAKH1A800010.pdf contains "FAKH1". It is important that only the filename is evaluated.
It must be done with C# regex, so please no "Contains"
You might be wondering why regex, but this is going to be used in a generic c# application that can evaluate regex expressions.
Thank you in advance.
You can try to use \\\w*(FAKH)\w*\.pdf pattern.
bool isExsit = Regex.IsMatch(#"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", #"\\\w*(FAKH)\w*\.pdf");
EDIT
You can use Groups[1].Value get FAKH
var result = Regex.Match(#"D:\DataFiles\Files_81\aars2016FAKH1800010.pdf", #"\\\w*(FAKH)\w*\.pdf");
var FAKH = result.Groups[1].Value;
c# online

Check if a string only contains the characters > or < or - [duplicate]

This question already has answers here:
.NET Regex Error: [x-y] range in reverse order
(3 answers)
How to match hyphens with Regular Expression?
(6 answers)
Closed 4 years ago.
What I need is to check if a string only contains the characters > or < or -.
So I thought using a RegEx for this, and I found an SO question with the exact same problem, and it has an answer (not the accepted one but the answer with regex)
This is the SO question : String contains only a given set of characters
So I modified the expression in this question to fit my needs like this :
static readonly Regex Validator = new Regex(#"^[><- ]+$");
and I call it like this ;
Validator.IsMatch(testValue)
But it's throwing the error
x-y range in reverse order
There are lots of question on SO about this error but I cant find or understand the answer I need.
So what am I doing wrong with this RegEx?
^[-<>]+$ "-" must come first in C# regex
Escape - within character groups. ([0-9] means "zero to nine" and not "zero, dash or nine")

How to match 2 substrings in responce with regex in c# [duplicate]

This question already has answers here:
Returning only part of match from Regular Expression
(4 answers)
Closed 4 years ago.
I have a responce string
"c=2020&action=approvecomment&_wpnonce=7508ac918a' data-wp-lists='dim:the-comment-list:comment-2020:unapproved:e7e7d3:e7e7d3:new=approved"
Im trying to extract 2020 and 7508ac918a. I dont understand how I must use regex with substrings in C#, simple regex like
c=(\d+)&action=approvecomment&_wpnonce=(.*?)' .+new=approved.
In Regex, you can create match groups
They look like this (?.+?)
So your _wpconce part could become something like this (?.*?)
Then you can grab each group individually for example
Match result = myRegex.Match(someString);
soneOtherString = result.Groups["GROUPNAME"].Value;
I use Regex101 to build and test my regex. (Whoever made that site deserves a crown with shinny stones on it!! :)
https://regex101.com/
Hope this helps

What regex expression would be appropriate? [duplicate]

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.

Categories

Resources