This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 8 years ago.
I have code in console app
reg = new Regex(#"/[a-z0-9_\-\+]+#[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i");
string text = "wjeqklejqwek myEmail#hotmail.com a;lekqlwe anothermail#mail.ru";
parseTextByTagName("", text);
MatchCollection coll = reg.Matches(text);
}
when I debug it it shows that the coll is empty could you tell whats problem I am solving it about an hour
try this
string strRegex = #"[A-Za-z0-9_\-\+]+#[A-Za-z0-9\-]+\.([A-Za-z]{2,3})(?:\.[a-z]{2})?";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"wjeqklejqwek myEmail#hotmail.com a;lekqlwe anothermail#mail.ru";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
Your regular expression worked for me if I take out / & /i.
[a-z0-9_\-\+]+#[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?
alternatively, you can also use this...
/^[\w-\._\+%]+#(?:[\w-]+\.)+[\w]{2,6}$/
Related
This question already has answers here:
Why \b does not match word using .net regex
(2 answers)
Closed 3 years ago.
I'm using Visual C# to create a little tool to support some people doing simple text manipulations. The tool has a GUI but uses regex in the code. Most things work already but now I found a problem that I'm not able to solve. I want to find the string MY2020 at the start of a word. Here's my code:
string TestString = "we have the string MY2020 somewhere in the line";
string Pattern = "\bMy2020";
RegexOptions options = RegexOptions.IgnoreCase;
MatchCollection myMatches = Regex.Matches(TestString, Pattern, options);
if (myMatches.Count > 0)
{
I expect MY2020 to be found. So myMatches.Count should be 1, but it's 0.
In parallel I use an online regex tester (https://regex101.com/). This one shows a match.
What am I missing?
You need to escape the "\", I have modified your code as
string TestString = "we have the string MY2020 somewhere in the line";
string Pattern = #"\bMy2020";
RegexOptions options = RegexOptions.IgnoreCase;
MatchCollection myMatches = Regex.Matches(TestString, Pattern, options);
if (myMatches.Count > 0)
This question already has answers here:
C#: How to Delete the matching substring between 2 strings?
(6 answers)
Closed 4 years ago.
I am trying to write the RegEx for replacing "name" part in below string.
\profile\name\details
Where name: -Can have special characters
-No spaces
Let's say I want to replace "name" in above path with ABCD, the result would be
\profile\ABCD\details
What would be the RegEx to be used in Replace for this?
I have tried [a-zA-Z0-9##$%&*+\-_(),+':;?.,!\[\]\s\\/]+$ but it's not working.
As your dynamic part is surrounded by two static part you can use them to find it.
\\profile\\(.*)\\details
Now if you want to replace only the middle part you can either use LookAround.
string pattern = #"(?<=\\profile\\).*(?=\\details)";
string substitution = #"titi";
string input = #"\profile\name\details
\profile\name\details
";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
Or use the replacement patterns $GroupIndex
string pattern = #"(\\profile\\)(.*)(\\details)";
string substitution = #"$1Replacement$3";
string input = #"\profile\name\details
\profile\name\details
";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
For readable nammed group substitution is a possibility.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 6 years ago.
I couldn't seem to find a match for this question so my apologies in advance if it's a duplicate.
Given the template: <<FirstName>> << FirstName >>
I want to replace both strings between the '<<>>''s using a single regex that should match both.
The following code doesn't seem to work the way I'm expecting:
[Test]
public void ShouldReplaceMultiple()
{
var pattern = "<<.*FirstName.*>>";
var template = "<<FirstName>> <<FirstName>>";
var replaceWith = "FOO";
var regex = new Regex(pattern);
Assert.AreEqual("FOO FOO", regex.Replace(template, replaceWith));
}
The output of the test is as follows:
Expected string length 7 but was 3. Strings differ at index 3.
Expected: "FOO FOO"
But was: "FOO"
--------------^
I don't understand why both strings won't be replaced?
Make it non-greedy using .*?
var pattern = "<<.*?FirstName.*?>>";
var template = "<<FirstName>> <<FirstName>>";
var replaceWith = "FOO";
var regex = new Regex(pattern);
Console.WriteLine(regex.Replace(template, replaceWith));
Ideone Demo
If you want to deal only with spaces in between the <<>>, then this will suffice
<<\s*?FirstName\s*?>>
string pattern = #"<<(?<=<<)\s*FirstName\s*(?=>>)>>";
var template = "<<FirstName>> <<FirstName>>";
var replaceWith = "FOO";
var regex = new Regex(pattern);
Assert.AreEqual("FOO FOO", regex.Replace(template, replaceWith));
This question already has answers here:
How to get number from a string
(5 answers)
Closed 7 years ago.
I am quite new to regular expression. My requirement is to extract number from string that includes mix of numbers and characters. I have tried below codes but I can only get the first number from string.
String serialNumber= "000745 TO 000748,00050-00052"
Match match = Regex.Match(serialNumber), #"(\d)+", RegexOptions.IgnoreCase);
if (match.Success)
{
int a = Convert.ToInt32(match); // This part not sure how to do
}
Expected result is:
000745
000748
00050
00052
string strRegex = #"\d+";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = #"000745 TO 000748,00050-00052";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}
You need to loop through your matches to get all the matches.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using a regular expression to validate an email address
Email Validation - Regular Expression
I am using following code to validate Email address in C#, but not sure why it fails always:
var regEx = #"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})";
if (Regex.IsMatch(regEx, "abcde#gmail.com", RegexOptions.IgnoreCase))
return true;
else
return false;
Please can someone point out what I am missing here ?
Try this:
[Test]
public void EmailTest()
{
var pattern = #"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
var address = "abcde#gmail.com";
Assert.IsTrue(Regex.IsMatch(address, pattern,RegexOptions.IgnoreCase));
Assert.IsTrue(regex.IsMatch(address));
}
You have the wrong order of string and pattern.
Also check this thread
You've got your IsMatch parameters the wrong way around; the first is the input, the second is the pattern.