Regex in C#

M

mick

Taking a few tentative steps into regex. Given this string -

"Northamptonshire T20 251/10 v Yorkshire T20 136/10 & 237 *"

I want to match "251/10 " and "136/10 " and "& 237 *" so I can remove them.

In these two regex testers -
http://www.regexr.com/
and http://regexpal.com/

the pattern "\d+\/\d+\s|\&\s\d+\s\*" does the trick but when I try in C#

var newString = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*","");

it doesn't remove the final part "& 237 *"

Are there differences in the .net implementation? Anyone give a hint as
to where I'm going wrong?

mick
 
A

Anders Eriksson

Taking a few tentative steps into regex. Given this string -

"Northamptonshire T20 251/10 v Yorkshire T20 136/10 & 237 *"

I want to match "251/10 " and "136/10 " and "& 237 *" so I can remove them.

In these two regex testers -
http://www.regexr.com/ and http://regexpal.com/

the pattern "\d+\/\d+\s|\&\s\d+\s\*" does the trick but when I try in C#

var newString = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*","");
it doesn't remove the final part "& 237 *"
Are there differences in the .net implementation? Anyone give a hint as
to where I'm going wrong?

mick

NOT an expert on Regex!!!

Using Expresso I get this code

private static void Main(string[] args)
{
Regex MyRegex = new Regex(
"\\d+\\/\\d+\\s|\\&\\s\\d+\\s\\*",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string InputText = "Northamptonshire T20 251/10 v Yorkshire
T20 136/10 & 237 *";
string MyRegexReplace = string.Empty;

// Replace the matched text in the InputText using the
replacement pattern
string result = MyRegex.Replace(InputText, MyRegexReplace);
}

and it works! Leaves a blank last but...

// Anders
 
M

mick

Taking a few tentative steps into regex. Given this string -

"Northamptonshire T20 251/10 v Yorkshire T20 136/10 & 237 *"

I want to match "251/10 " and "136/10 " and "& 237 *" so I can remove them.

In these two regex testers -
http://www.regexr.com/ and http://regexpal.com/

the pattern "\d+\/\d+\s|\&\s\d+\s\*" does the trick but when I try in C#

var newString = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*","");
it doesn't remove the final part "& 237 *"
Are there differences in the .net implementation? Anyone give a hint as
to where I'm going wrong?

mick

NOT an expert on Regex!!!

Using Expresso I get this code

private static void Main(string[] args)
{
Regex MyRegex = new Regex(
"\\d+\\/\\d+\\s|\\&\\s\\d+\\s\\*",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string InputText = "Northamptonshire T20 251/10 v Yorkshire
T20 136/10 & 237 *";
string MyRegexReplace = string.Empty;

// Replace the matched text in the InputText using the
replacement pattern
string result = MyRegex.Replace(InputText, MyRegexReplace);
}

and it works! Leaves a blank last but...
Taking a few tentative steps into regex. Given this string -

"Northamptonshire T20 251/10 v Yorkshire T20 136/10 & 237 *"

I want to match "251/10 " and "136/10 " and "& 237 *" so I can remove them.

In these two regex testers -
http://www.regexr.com/ and http://regexpal.com/

the pattern "\d+\/\d+\s|\&\s\d+\s\*" does the trick but when I try in C#

var newString = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*","");
it doesn't remove the final part "& 237 *"
Are there differences in the .net implementation? Anyone give a hint as
to where I'm going wrong?

mick

NOT an expert on Regex!!!

Using Expresso I get this code

private static void Main(string[] args)
{
Regex MyRegex = new Regex(
"\\d+\\/\\d+\\s|\\&\\s\\d+\\s\\*",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string InputText = "Northamptonshire T20 251/10 v Yorkshire
T20 136/10 & 237 *";
string MyRegexReplace = string.Empty;

// Replace the matched text in the InputText using the
replacement pattern
string result = MyRegex.Replace(InputText, MyRegexReplace);
}

and it works! Leaves a blank last but...

Yes, it does seem to. Any idea why the first one doesn't? I'm going to have to
check out what those flags do. Cheers.

mick
 
A

Arne Vajhøj

Taking a few tentative steps into regex. Given this string -

"Northamptonshire T20 251/10 v Yorkshire T20 136/10 & 237 *"

I want to match "251/10 " and "136/10 " and "& 237 *" so I can remove them.

In these two regex testers -
http://www.regexr.com/ and http://regexpal.com/

the pattern "\d+\/\d+\s|\&\s\d+\s\*" does the trick but when I try in C#

var newString = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*","");
it doesn't remove the final part "& 237 *"
Are there differences in the .net implementation? Anyone give a hint as
to where I'm going wrong?

I can not reproduce your problem.

using System;
using System.Text.RegularExpressions;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
string s = "Northamptonshire T20 251/10 v Yorkshire T20
136/10 & 237 *";
string s2 = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*", "");
Console.WriteLine("|" + s2 + "|");
string s3 = Regex.Replace(s,@"\d+/\d+\s|&\s\d+\s\*", "");
Console.WriteLine("|" + s3 + "|");
string s4 = Regex.Replace(s,@"\s\d+/\d+|\s&\s\d+\s\*", "");
Console.WriteLine("|" + s4 + "|");
Console.ReadKey();
}
}
}

outputs:

|Northamptonshire T20 v Yorkshire T20 |
|Northamptonshire T20 v Yorkshire T20 |
|Northamptonshire T20 v Yorkshire T20|

here (.NET 4.5.1).

Arne
 
M

mick

Taking a few tentative steps into regex. Given this string -

"Northamptonshire T20 251/10 v Yorkshire T20 136/10 & 237 *"

I want to match "251/10 " and "136/10 " and "& 237 *" so I can remove them.

In these two regex testers -
http://www.regexr.com/ and http://regexpal.com/

the pattern "\d+\/\d+\s|\&\s\d+\s\*" does the trick but when I try in C#

var newString = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*","");
it doesn't remove the final part "& 237 *"
Are there differences in the .net implementation? Anyone give a hint as
to where I'm going wrong?

I can not reproduce your problem.

using System;
using System.Text.RegularExpressions;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
string s = "Northamptonshire T20 251/10 v Yorkshire T20 136/10 &
237 *";
string s2 = Regex.Replace(s,@"\d+\/\d+\s|\&\s\d+\s\*", "");
Console.WriteLine("|" + s2 + "|");
string s3 = Regex.Replace(s,@"\d+/\d+\s|&\s\d+\s\*", "");
Console.WriteLine("|" + s3 + "|");
string s4 = Regex.Replace(s,@"\s\d+/\d+|\s&\s\d+\s\*", "");
Console.WriteLine("|" + s4 + "|");
Console.ReadKey();
}
}
}
outputs:

|Northamptonshire T20 v Yorkshire T20 |
|Northamptonshire T20 v Yorkshire T20 |
|Northamptonshire T20 v Yorkshire T20|

here (.NET 4.5.1).

This is bizarre. I can't reproduce the problem today either. I get the result I
expected.
Very strange.

mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top