Hello programmers!

I have another easy problem. I want to write code that can find a word in the string, that contains another substring.

Code needs to get or just copy specific word in string. To get that word, this word need to contain some specific string (ex: "Platypus"). If some text has a word that says PerryThePlatypus, I want to get word PerryThePlatypus in my textbox or anywhere else.

Thanks.

Dani AI

Generated

Short summary: wanted to extract the whole word that contains a given substring (for example, find "PerryThePlatypus" because it contains "Platypus"). 's Split-based approach is simple and works for basic space-separated text, but that implementation overwrites the return value so it ends up returning only the last match and it breaks on punctuation/line breaks. correctly points out that regular expressions are the right tool; the posted character class is a bit narrow for real-world text (it only covers a small set of characters).

A practical, robust pattern is to let Regex locate words that contain the escaped substring and return all matches (optionally case-insensitive). The code below returns a List(Of String) of distinct matches in the order they appear:

Function FindWordsContaining(text As String, subStr As String, Optional caseInsensitive As Boolean = True) As List(Of String)
    If String.IsNullOrEmpty(subStr) Then Return New List(Of String)
    Dim opts As RegexOptions = If(caseInsensitive, RegexOptions.IgnoreCase, RegexOptions.None)
    Dim pattern As String = "\b\w*" & Regex.Escape(subStr) & "\w*\b"
    Dim matches = Regex.Matches(text, pattern, opts)
    Dim results As New List(Of String)
    For Each m As Match In matches
        If Not results.Contains(m.Value) Then results.Add(m.Value)
    Next
    Return results
End Function

Notes and troubleshooting:

  • Use Regex.Escape on the substring so characters like "." or "+" are treated literally.
  • \w covers letters, digits and underscore; if you need hyphens, apostrophes or Unicode letters, adjust the character class (for example, include \p{L} or add '-).
  • To preserve duplicates remove the Contains check; to get just the first match, use Regex.Match.
  • For very large texts or repeated queries, add RegexOptions.Compiled when building the regex for better performance.
  • If you only want exact whole-word matches, match with word boundaries around the escaped substring (no surrounding \w*).

Recommended Answers

All 3 Replies

Function getWord(ByVal myStr As String, ByVal subStr As String) As String
        Dim strWords As String() = myStr.Split(" ")
        Dim retWord As String = ""
        For Each word As String In strWords
            If word.Contains(subStr) Then
                retWord = word
            End If
        Next
        Return retWord
    End Function

Well using this you will get only one word from string.

It works.

Thanks! ;)

You can use regular expressions to step through every word. If you define a word as any string of one or more

  • lower case letter
  • upper case letter or
  • single quote

then to read a file and list all the words you can do

Dim text As String = My.Computer.FileSystem.ReadAllText(txtMyFile.Text)
Dim rex As New Regex("[a-z,A-Z,']+")

For Each m As Match In rex.Matches(text)
    Debug.WriteLine(m.Value)
Next

This will list all of the words, one per line. You should be able to check each word to see if it contains the substring. The expression, [a-z,A-Z,']+ matches one or more (the plus sign) of any combination of lower case letters (a-z), upper case letters (A-Z) or single quotes. I don't know if it is possible to have a word with more than one single quote, but it will match those as well. Note - you have to import System.Text.RegularExpressions

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.