Regular Expression Issue/Question

  • Thread starter Thread starter Stephen Costanzo
  • Start date Start date
S

Stephen Costanzo

I have inherited an application that uses Regular Expressions to find items
out of a data string and I am getting an odd result. It appears the regular
expression engine is grabbing the wrong value. For ease of the post I have
simply grabbed the relevant pieces of code without comments.

Dim Data As String = "fileCountThreshold=10, fileSizeThreshold=512,
foldername=C:\logs\, " & _
"fileSizeUnit=kilobytes, fileAgeThreshold=1, fileAgeUnit=months,
addMethod=true, " & _
"addPidTid=false, fileNameTemplate='console-{1:MMM-yy}-{0:0000}.log'"

Dim defaultValue As Boolean = False

Dim m As Match = Regex.Match(Data, "(?<=addPidTid=)false|true",
RegexOptions.IgnoreCase)

If m.Success Then
Debug.WriteLine(Boolean.Parse(m.Value))
Else
Debug.WriteLine(defaultValue)
End If

The m value returns the fact that it was successful in finding the value and
it indicates that the value found is true. However the value is true for the
addMethod part of the data. Other values include:
m.Index = 141 (which points to the first true in the line)
m.Length = 4
m.Captures.Count=1

If I put this into the code (following the first check)
Dim m1 As Match = Regex.Match(Data,
"(?<=fileAgeThreshold=('|"")*)[^'"",]+", RegexOptions.IgnoreCase)
If m1.Success Then
Debug.WriteLine(m1.Value)
Else
'-- check for the ='' ="" =, case (empty string)
If Regex.IsMatch(Data, Name & "=['"",]['""]*", RegexOptions.IgnoreCase)
Then
Debug.WriteLine("")
Else
Debug.WriteLine(sdefault)
End If
End If

It correctly finds the value 1.

Any help would be appreciated.
 
Stephen said:
Dim m As Match = Regex.Match(Data, "(?<=addPidTid=)false|true",
RegexOptions.IgnoreCase)

The problem is that the | operator binds too strongly. You need to use
another pair of parens to make it work:

(?<=addPidTid=)(false|true)



Oliver Sturm
 
Back
Top