Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
We need to validate password against following pattern.
“Xabcdef99*” [1st char uppercase, 2nd to 7th chars lowercase, 8th to 9th digits and last char a symbol ]
Can someone provide me Regex for the same? How I can validate following password against that Regex in C#.
Userpcs12* --> Valid
Testeur333 --> Invalid (because last char is not symbol)
userpcs12* --> Invalid (because first char is not uppercase)
You may try this,
^[A-Z][a-z]{6}\d{2}[~!##$%^&*]$
Add the symbols you want inside the last character class.
or
^[A-Z][a-z]{6}\d{2}\W$
\W matches any non-word character. Change this to [\W_] if you treat _ as special charcater.
Try this:
Regex regex = new Regex(#"^[A-Z][a-z]{6}\d{2}\W$");
Assert.IsTrue(regex.IsMatch("Xabcdef99*"));
Assert.IsTrue(regex.IsMatch("Xabcdef99$"));
Assert.IsFalse(regex.IsMatch("Testeur333"));
Assert.IsFalse(regex.IsMatch("userpcs12*"));
Check this regex:
\A\p{Lu}\p{Ll}{6}\d{2}[~!##$%^&*]\z
\p{Lu} matches uppercase letters
\p{Ll} matches lowercase letters
Demo on regex101
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have been trying to make a regex expression which can match all the below strings. I was able to make a regex for first two, but unable to do for the next 2. The regex should be such that the matching is performed only until the end of error message.
RAISERROR 20001 #errmsg;
RAISERROR 20001 #errmsg
RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg'
RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg';
Below is the test link:
https://regex101.com/r/ntywvP/3
((?i)raiserror)\s*\d{5,6}\s*([^;|\s*]*)
Any help is appreciated. Thank you in advance.
You could replace ([^;|\s*]*) by (#[^;\r\n]*). You could try the following regex.
(?i)raiserror\s*\d{5,6}\s*(#[^;\r\n]*)
Demo
I would use this:
\bRAISERROR \d+ #(?:'[\w ]+'|[\w ]+);?
Demo
I deal with the optional single quoted message by using an alternation which allows either for this, or for a message sans single quotes.
My suggestion is https://regex101.com/r/ntywvP/4
^((?i)raiserror)\s*\d{5,6}\s*(.+?)(;|\s*)$
Capturing the string beginning with # is done via matching a s few characters as possible with (.+?). This allows specifying possible characters after the wildcard before the end of the line.
^ matches line start
$ matches line end
You can match all between the single quotes or match only non whitespace chars. If you don't need the capturing group around ((?i)raiserror) you can omit it.
(?i)raiserror\s*\d{5,6}\s*#(?:'[^'\r\n]*'|\S+);?
Explanation
(?i) case insensitive modifier
raiserror\s*\d{5,6}\s* Match raiserror and 5-6 digits between optional whitespace chars
# Match the # char
(?: Non capture group for the alternation
'[^'\r\n]*' Match any char except ' between an opening and closing '
| Or
\S+ Match 1+ non whitespace chars
);? Close group and match optional ;
Regex demo
Based on your question you can use either:
'.'
This matches everything in the world with at least one character, so it will match all four of your strings.
Or to match the four strings exactly:
'RAISERROR 20001 #errmsg|RAISERROR 20001 #errmsg|RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg|RAISERROR 20001 #'ajhsgdjh jahsgdjahsgdjg'
You haven't specified the rules for what strings you want to match and what strings you don't. Any other solutions require guessing on what you mean but haven't explained.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a scenario in C#:
Input is->
Sachin.Dutta : trimbegging space :: delete spaceafter
Expected output->
Sachin.Dutta:trimbegging space::delete spaceafter
First,I need to split the string based on : or ::
Then trim whitespaces from beginning and end of every word
Then combine the words to get expected output.
I can write code using for loop to separate words and again recombine them.But,is there any better way using LINQ or Regex for this ?
Use Regex with pattern #"\s*:\s*"
var input = "Sachin.Dutta : trimbegging space :: delete spaceafter";
var result = Regex.Replace(input, #"\s*:\s*", ":");
Output
Sachin.Dutta:trimbegging space::delete spaceafter
Explanation
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case sensitive)
\s* matches any whitespace character (equal to [\r\n\t\f\v ])
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have to check if string contains only lowercase letters, from 'd' onwards, and the symbols '{', '}', '|', '#'.
I have tried to create Regex but it matches substrings which is not the desired behaviour (the second test mustn't match in the example which I add). I don't know how to explain that the order isn't important and the strings must contain only of the allowed characters. Regex101
[d-z]+[{}|#]+
This regex matches arx#vkdww#qrw#sdvv and this isn't right. How can I fix it?
Thank you in advance!
Your pattern [d-z]+[{}|#]+ matches 1+ times a char d-z in a character class followed by one of the chars listed in the character class [{}|#]+
You have to use anchors to assert the start ^ and end $ of the string and use 1 character class containing all the allowed characters instead of 2:
^[d-z{}|#]*$
Regex demo
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I need a Reqular expression to identify string in following format in my C# code. The string will always start with "REG" and contain 3 underscores with 2 words and one number between the underscores. See below example:
Example: "REG_SOFTWARE_SECURITY_1234"
I used below REGEX expression suggested by your forums:
"\b[a-zA-Z0-9_]+\b"
But it passes the incorrect inputs also like:
REG_1234
So, it should only pass input in format - "REG_SOFTWARE_SECURITY_1234" Any suggestions?
I dont see issue with your regex. You might be using it incorrectly in c#.
Try this
var str="ALPHABET_ ALPHABET _ ALPHABET _99";
var res = Regex.Matches(str,#"\b[a-zA-Z0-9_]+\b");
foreach (Match match in res)
{
Console.WriteLine(match.Value);
}
Fiddle Here
The backslashes in your search string are not being interpreted as you expect. The C# string "\b[a-zA-Z0-9_]+\b" starts and ends with a "backspace" 0x0008 character.
To match the example string you need to use either "\\b[a-zA-Z0-9_]+\\b" or #"\b[a-zA-Z0-9_]+\b" as the regular expression.
If the task is to match a string with two words and one number separated by three underscores then the following should work:
#"\b[a-zA-Z]+_[a-zA-Z]+_[a-zA-Z]+_[0-9]+\b"
Did you mean a letter by ALPHABET ?
then you could try \w+_ \w+ _ \w+ _\d+, if you intended whitespaces, and you are searching for words, not just letters.
If you want to find a pattern like Letter Underscore Blank etc. , try \w_ \w _ \w _\d.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to parse the email address from a mailto tag. I am looking for a way to do this via RegEx in C#.
Example input:
<mailto:abc#xyz.com>
Example output:
abc#xyz.com
In general, it's a very bad idea to use regular expressions for HTML parsing. Instead, take a look at the Html Agility Pack. For the specific input you provided, you may use:
(?<=\<mailto:).*(?=\>)
Here's a code sample:
var emailTag = "<mailto:abc#xyz.com>";
var emailValue = Regex.Match(emailTag, #"(?<=\<mailto:).*(?=\>)").Value;
Console.WriteLine(emailValue);
A simple Regex to strip anything in a mailto tag would be
<mailto:(.*?)>
You could use:
[\w\d]+\#[\w\d]+\.com
[\w\d] <----This matches any letter or character. \w matches any letter. \d matches anynumber.
+ <----One or more of previous item, in this case [\w\d]+ one or more letters or numbers
\# <----Simply matches the # symbol but it needs to be escaped with a \ as it is a special character
[\w\d]+ <----Same again
\. <---- Same concept as the # as . is a special character so it needs to be escaped
In your example:
[\w\d]+=abc
\#=#
[\w\d]+=xyz
\.=.
com=com
If your wanting to match special characters as well as letters and digits then just replace [\w\d]+ with [\S]+ (make sure s is capital).
[\S]+ <---Matches anything that is not a space.
You will have to do variations to include .co.uk and .org etc.
http://www.regular-expressions.info/reference.html <----This is very useful!