1

I'm not sure I'm asking what is possible. I have a situation

Dim animalList as string = "Dog|Cat|Bird|Mouse"
Dim animal_story_string as string = "One day I was walking down the street and I saw a dog"
Dim hasAnAnimalonList as Boolean 
Dim animals() as String =   animalList.Split("|")
Dim regex As New Regex("\b" & String.Join("\b|\b", animals) & "\b", RegexOptions.IgnoreCase)
If regex.IsMatch(animal_story_string) Then
   hasAnAnimalonList = True
  'Here I would like to replace/format the animal found with HTML bold tags so it would look 
   like  "One day I was walking down the street and I saw a <b>dog</>"
End If

In the past I would loop each value in the animalList and if a match is found replace it at that time. Like

     For Each animal As string in animals
      ' I would replace every animal in the list
      ' If Cat and Birds and Mouse were not in the string it did not matter 
        animal_story_string = animal_story_string.Replace(animal,"<b>" + animal + "</b>" 
     Next

Is there a way to do it using a Regex function?

2 Answers 2

1

Is there a way to do it using a Regex function?

Yes, call Regex.Replace method and split the Dog|Cat|Bird|Mouse string to join the result and create the regex pattern as shown below, you can replace the matches in one line using a MatchEvaluator function.

Dim animalList = "Dog|Cat|Bird|Mouse"
Dim regexPattern = String.Join("|", animalList.Split("|"c).Select(Function(x) $"\b{x}\b"))
Dim animal_story_string = "One day I was walking down the street and I saw a dog or maybe a fat cat! I didn't see a bird though."
Dim hasAnAnimalonList = Regex.IsMatch(animal_story_string, regexPattern, RegexOptions.IgnoreCase)

If hasAnAnimalonList Then
    Dim replace = Regex.Replace(
        animal_story_string,
        regexPattern,
        Function(m) $"<b>{m.Value}</b>", RegexOptions.IgnoreCase)

    Console.WriteLine(replace)
End If

Writes in the console:

One day I was walking down the street and I saw a <b>dog</b> or maybe a fat <b>cat</b>! I didn't see a <b>bird</b> though.

... and in HTML renderer...

One day I was walking down the street and I saw a dog or maybe a fat cat! I didn't see a bird though.

Sign up to request clarification or add additional context in comments.

5 Comments

This looks great-- I'll give it a try Thanks Regex is extremely powerful
@Bill I've fixed the regex pattern to not match for example dog in hotdog...etc
Thank you. I actually have a use for both situations. As you can imagine, the Cat's and Dog's are just a simplified example. I'll push my luck and ask do you have a simple way for Regex to handle and &? So if I wanted to format in a sentence "Bob & Mary" but the list has "Bob and Mary" . Currently in the sentence I test if & exists and if it does I add to the list an additional item with replacing "and" with "&". I highlight format if a researcher publishes using their department of "Cell and Molecular Pharmacology" or "Cell & Molecular Pharmacology". Have no control over entry
@Bill You are welcome. I'm not sure if you mean something like this or an explicit one like this. It Depends, maybe a string method call like: Dim s = "Cell and Molecular Pharmacology".Replace("and", "&") could be more efficient in some cases. Keep it simple.
Yup-- I'm doing the replace and that's working fine. Very similar to matching joined combined names with or without hyphens like: Smith Jones and Smith-Jones same person just a different input. Thanks for your help
1

I think

/(?:^|(?<= ))(Dog|Cat|Bird|Mouse)(?:(?= )|$)/i

or even

/\b(Dog|Cat|Bird|Mouse)\b/i

See: https://regex101.com/r/V4Uhg7/1

will do what you want?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.