Use the RegexOptions.IgnoreCase
flag:
Regex.Replace(strNote, strPattern, strReplacement, RegexOptions.IgnoreCase)
If you are going to ignore case there should be no need to use LCase
. I also find it odd that you have all those $
symbols in your variable names - they shouldn't be valid in either C# or VB.NET.
EDIT #2: I realize you may have wanted to replace the entire line that matched with the
$1
replacement pattern to match the ID. If you have a need to use multiple options you can
Or
them together as follows:
Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
EDIT #1: you are using the wrong method to extract the ID. You have a group (\d+)
to capture the ID, but you are using Regex.Replace
on your match, which is why you get everything else in the text. To match the ID use the following:
Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(\d+).*"
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
If m.Success Then
Dim id As String = m.Groups(1).Value
Console.WriteLine("ID: " & id)
Else
Console.WriteLine("No Match!")
End If
You will notice we refer to Groups(1)
which holds the value captured by the (\d+)
group. Patterns with more groups may lead to confusion, especially with nested groups. In those cases you can use named groups. Here is the same code updated to use named groups:
Dim input As String = "foo web id:2010 bar"
Dim pattern As String = ".*web id: ?(?<ID>\d+).*" ' group name added '
Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
If m.Success Then
' refer to group by group name '
Dim id As String = m.Groups("ID").Value
Console.WriteLine("ID: " & id)
Else
Console.WriteLine("No Match!")
End If