OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Looping through list for a pattern match with strings with wildcards

  • Thread starter Thread starter Kerry Harp
  • Start date Start date
K

Kerry Harp

Guest
I have sample code of a function in which uses pattern matching to match a string value containing wildcards with an actual string value.

Code:
def isMatch(text, pattern):
    n = len(text)
    m = len(pattern)
    i = 0
    j = 0
    startIndex = -1
    match = 0

    while i < n:
        if j < m and (pattern[j] == '?' or pattern[j] == text[i]):
            i += 1
            j += 1
        elif j < m and pattern[j] == '*':
            startIndex = j
            match = i
            j += 1
        elif startIndex != -1:
            j = startIndex + 1
            match += 1
            i = match
        else:
            return False

    while j < m and pattern[j] == '*':
        j += 1

    return j == m


s = "RE340T6-1MCWW"
p = "RE340T*-*****"

if isMatch(s, p):
    print("Yes")
else:
    print("No")

In this sample code, the string (s) "RE340T6-1MCWW" matches the pattern (p), "RE340T*-*****" and returns a "Yes", as it should.

What I want to do is using the above function,have a list of strings and find the matches in a list of strings with wildcards. For example:

Code:
s = ['GUC 40 410','HUC 40 410','XUC 40 410']
p = ['GUC 40 4**','HUC 40 4**','XUC 40 4**']

I don't know how to insert looping through the lists in the function to return where the string (s) matches the pattern (p). Any ideas for the next step?

If I just substitute the list for the static string, it just returns a "No"
<p>I have sample code of a function in which uses pattern matching to match a string value containing wildcards with an actual string value.</p>
<pre><code>def isMatch(text, pattern):
n = len(text)
m = len(pattern)
i = 0
j = 0
startIndex = -1
match = 0

while i < n:
if j < m and (pattern[j] == '?' or pattern[j] == text):
i += 1
j += 1
elif j < m and pattern[j] == '*':
startIndex = j
match = i
j += 1
elif startIndex != -1:
j = startIndex + 1
match += 1
i = match
else:
return False

while j < m and pattern[j] == '*':
j += 1

return j == m


s = "RE340T6-1MCWW"
p = "RE340T*-*****"

if isMatch(s, p):
print("Yes")
else:
print("No")
</code></pre>
<p>In this sample code, the string (s) <code>"RE340T6-1MCWW"</code> matches the pattern (p), <code>"RE340T*-*****"</code> and returns a <code>"Yes"</code>, as it should.</p>
<p>What I want to do is using the above function,have a list of strings and find the matches in a list of strings with wildcards. For example:</p>
<pre><code>s = ['GUC 40 410','HUC 40 410','XUC 40 410']
p = ['GUC 40 4**','HUC 40 4**','XUC 40 4**']
</code></pre>
<p>I don't know how to insert looping through the lists in the function to return where the string (s) matches the pattern (p). Any ideas for the next step?</p>
<p>If I just substitute the list for the static string, it just returns a "No"</p>
 

Latest posts

Top