I am having an issue with regexp in C#.
I’ve been using theses patterns in F# and it works fine, so i don’t understand why it would’nt work in C#.
So let’s say i’ve got a muline input file. I need to parse this file for specific data:
Exemple:
Lorem ipsum dolor sit amet, consectetur adipiscing elit (Token1 : 42)
Aliquam id ante ut ante tempus fringilla Token2 (ante ut ) : 45
Morbi varius adipiscing lacus, eget pellentesque tellus vulputate Token3 : 43
I basicaly need to retrieve the numbers written after Token1, Token2 , Token3 in a single match ( ie just want my number as a result).
The patterns i used in F# are the following ones:
PatternToken1 = "(?:Token1 : )(\d+)"
PatternToken2 = "(?:Token2.* : )(\d+)"
PatternToken3 = "(?:Token3 : )(\d+)"
So my issue is the following one :
pattern matching my input string in F# would give me the following results:
MatchedToken1 = 42
MatchedToken2 = 45
MatchedToken3 = 43
In C# i would get the following results:
MatchedToken1 = Token1 : 42
MatchedToken2 = Token2 (ante ut ) : 45
MatchedToken3 = Token3 : 43
How come this works in F# and not in C# ? What kind of pattern must i use for it to work in C#?
EDIT:
Here is the code i use to match my patterns in c#:
abstract class PatternMatcherBaseEntity<T>
{
protected Regex Pattern;
protected T Match;
private static TK Convert<TK>(string input)
{
TK res=default(TK);
var converter = TypeDescriptor.GetConverter(typeof(TK));
if(converter != null)
{
try
{
res = (TK) converter.ConvertFromString(input);
}
catch (Exception)
{
res = default(TK);
}
}
return res;
}
protected bool Matcher(string s)
{
var res = false;
//var matchedData = Regex.Match(s, Patterm);
var content = Pattern.Matches(s);
if(content.Count>0)
{
//Match = Convert<T>(content.Value);
Match = Convert<T>(content[0].Value);
res = true;
}
return res;
}
public T MatchGetter(String stringToMatch)
{
T ret = default(T);
if(stringToMatch != String.Empty)
{
ret = stringToMatch.Match()
.With(Matcher, x => Match)
.Else(x => default(T))
.Do();
}
return ret;
}
}
by the way i’ve tested using verbatim strings and escape string. It would not compile otherwise
Try using the following: