Home > Net >  How can I use a wildcard in a vb.net line.contains that is surrounded by double quotes
How can I use a wildcard in a vb.net line.contains that is surrounded by double quotes

Time:12-16

I have some vb.net code

   Dim reader As New StreamReader("C:\geoData\zipCodes.geojson", Encoding.Default)
    Dim line As String = Nothing
    Dim lines As Integer = 0
    While (reader.Peek() <> -1)

If line.Contains("""ZCTA5CE20"": ""47236""") Then

And it works perfectly finding that line in a 2.5 gig json file. However, I need to find all lines that contain

If line.Contains("""ZCTA5CE20"": ""47*""") Then

so I need a wildcard after the 47 but if I put in a * it is looking for a * followed by double quotes not a wildcard. I still need the double quotes before the 47.

I have code that works it in as a JSON object but if I try to load the entire file it throws a memory error.

    Dim json = File.ReadAllText("C:\geoData\zipCodes.geojson")
    Dim ResultArray As New JArray
    Dim Jvalues As JObject = JObject.Parse(json)

CodePudding user response:

You will need a Regex search.

Dim jsonRegex = New Regex("""ZCTA5CE20"":\ ""47.*?""", RegexOptions.Compiled)

If jsonRegex.IsMatch(line) Then
   ...
End If

You can use the File.ReadLines to loop through the lines one line at a time without loading the whole file at once.

Dim jsonRegex = New Regex("""ZCTA5CE20"":\ ""47.*?""", RegexOptions.Compiled)

For Each line As String In File.ReadLines("C:\geoData\zipCodes.geojson")
    If jsonRegex.IsMatch(line) Then
       ...
    End If
Next
  • Related