Skip to main content
GameDev.net gamedev.net
๐Ÿ”’ Locked

.NET Regex problem

Started by DarkSuicide Dec 26, 2006 at 10:59 PM 1 replies 600+ views
Original Post
DarkSuicide
DarkSuicide
Ok, so here is the Regex declaration:

public static Regex GliderCharacterHealthPattern = new Regex("^Health: \\d|\\d\\.\\d+$");
and here is the code to see if a string matches the Regex pattern:

else if(StringPatterns.GliderCharacterHealthPattern.IsMatch(data))
{
    _GliderStatus.CharacterHealth = float.Parse(data.Substring(data.IndexOf(':') + 2));
}
Now, for some reason when this string goes through the check "Location: -1615.49 -1997.51", it says it matches the pattern... Could someone please tell me why, as I am very new to Regex. Thanks for the help.
Thevenin
Thevenin
Does this work?

public static Regex GliderCharacterHealthPattern = new Regex("^(Health:)\\s\\d|\\d\\.\\d+$");

Also, you can name particular regions so that you can extract the values easily.

public static Regex GliderCharacterHealthPattern = new Regex("^(Health:)\\s(?\\d)|(?\\d)\\.\\d+$");

int TheNum1 = TheMatch.Groups["num1"].Value;
int TheNum2 = TheMatch.Groups["num2"].Value;
Thevenin
Thevenin
Also, the '.' has special meaning in regular expressions. If you intended it to be a dot, use this instead '[\\x2e]'.

Edit: Try this ... public static Regex GliderCharacterHealthPattern = new Regex(@"^(Health:)[\s][\d][\x7c][\d][\x2e][\d]+$");

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.