Matching Percent Symbol Using REGEX
-
Hello, I am trying to match a substring which contains the percent symbol using REGEX W character but it does not seem to work. Below is what I have so far.
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
string escapedString = Regex.Escape("border-bottom");
string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(matchedCss);
string matchedString = match.Value;
Console.WriteLine(matchedString);
Console.ReadLine();The REGEX pattern above should have matched the substring
border-bottom:1% solid black;
but for some reason it did not. Please help me resolve this problem, thanks in advance.
-
Hello, I am trying to match a substring which contains the percent symbol using REGEX W character but it does not seem to work. Below is what I have so far.
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
string escapedString = Regex.Escape("border-bottom");
string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(matchedCss);
string matchedString = match.Value;
Console.WriteLine(matchedString);
Console.ReadLine();The REGEX pattern above should have matched the substring
border-bottom:1% solid black;
but for some reason it did not. Please help me resolve this problem, thanks in advance.
You don't need to escape the colon maybe? And probably not the semi-colon either? And after all the begging I had to do to get Chris to add a Regular Expressions Forum[^] , too. :sigh:
-
You don't need to escape the colon maybe? And probably not the semi-colon either? And after all the begging I had to do to get Chris to add a Regular Expressions Forum[^] , too. :sigh:
Hi, thanks for your reply. I have updated my pattern by eliminating the back slash in front of the colon and semicolon as you have suggested. However, it still does not work. Below is the updated pattern
pattern = escapedString + @":\s?\d+\W(\s?\w+)*\s?;?\s?";
-
Hello, I am trying to match a substring which contains the percent symbol using REGEX W character but it does not seem to work. Below is what I have so far.
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
string escapedString = Regex.Escape("border-bottom");
string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(matchedCss);
string matchedString = match.Value;
Console.WriteLine(matchedString);
Console.ReadLine();The REGEX pattern above should have matched the substring
border-bottom:1% solid black;
but for some reason it did not. Please help me resolve this problem, thanks in advance.
ehm:
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}"; string escapedString = Regex.Escape("border-bottom"); string pattern = escapedString + @"\\:\\s?\\d+\\W(\\s?\\w+)\*\\;?\\s?"; Regex rgx = new Regex(pattern); Match match = rgx.Match(inputString); string matchedString = match.Value; Console.WriteLine(matchedString);
It works for me. At least when I set the inputString to match you expression.
-
Hi, thanks for your reply. I have updated my pattern by eliminating the back slash in front of the colon and semicolon as you have suggested. However, it still does not work. Below is the updated pattern
pattern = escapedString + @":\s?\d+\W(\s?\w+)*\s?;?\s?";
It works for
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
I put the code provided in VS and got:Error 1 The name 'matchedCss' does not exist in the current context F:\Project\PlayGround\Playground\Form1.cs 757 56 Playground
So, what text are you actually trying to match? -
ehm:
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}"; string escapedString = Regex.Escape("border-bottom"); string pattern = escapedString + @"\\:\\s?\\d+\\W(\\s?\\w+)\*\\;?\\s?"; Regex rgx = new Regex(pattern); Match match = rgx.Match(inputString); string matchedString = match.Value; Console.WriteLine(matchedString);
It works for me. At least when I set the inputString to match you expression.
-
It is now working. It was driving me nuts, after everything failed I decided to restart Visual Studio and it started working.
Rule 1: Whenever a problem goes away on its own, it will come back on its own.
-
It is now working. It was driving me nuts, after everything failed I decided to restart Visual Studio and it started working.