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.
Related
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
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
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!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a regular expression for parsing out individual sentences?
I want to split large text into sentence . The regex expression i got from answer here
string[] sentences = Regex.Split(mytext, #"(?<=[\.!\?])\s+");
So I thought of using a pattern to do splitting like
if a . ? ! follows a space and a capital letter than do the split.
Capital letter indicates starting of sentence .
text = " Sentence one . Sentence e.g. two ? Sentence three.
sentence[1] = Sentence one
sentence[2] = Sentence e.g. two
For problematic cases like abbreviations i intend to do replacing
mytext.replace("e.g.","eg");
How to implement this in regex ?
\p{Lt} indicates a Unicode uppercase letter (including accents etc.), so
string[] sentences = Regex.Split(mytext, #"(?<=[.!?])\s+(?=\p{Lt})");
should do what you want.
(Note that I don't think . or ? need to be escaped in a character class so I've removed them too, but do check that this still works with those characters.)
However, note that this will still split on e.g. Mr. Jones...