Home > Blockchain >  Placeholder variable for a string in Python that can take the form of any character
Placeholder variable for a string in Python that can take the form of any character

Time:05-28

is there some kind of method in python that would allow me something like this:

if string == "*"   "this"   "*"  "blue"   "*":

and it would be True if "string" was "this is blue", "this was blue" or "XYZ this XYZ blue XYZ"

is something like this possible in python in a simple way? I don't mean the %s format cause there you need to pass some value as %s, i need it to be able to check all possible forms resp. just ignore everything between "this" and "blue". However I can't just check if the first 4 characters of the string are "this" and the last 4 would be "blue" because the string is actually a long text and I need to be able to check if within this long text there is a part that says "this .... blue"

CodePudding user response:

Use a regex, which is provided in Python via the re module:

>>> import re
>>> re.match(".*this.*blue.*", "this is blue")
<re.Match object; span=(0, 12), match='this is blue'>

In a regex, .* has the wildcard effect you're looking for; . means "any character" and * means "any number of them".

If you wanted to do this without a regex, you could use the str.find method, which gives you the index of the first occurrence of a string within a larger string. First find the index of the first word:

>>> string = "did you know that this blue bird is a corvid?"
>>> string.find("this")
18

You can then slice the string to everything after that word:

>>> string[18 4:]
' blue bird is a corvid?'

and repeat the find operation with the second word:

>>> string[18 4:].find("blue")
1

If either find() call returns -1, there is no match. In the form of a function this might look like:

>>> def find_words(string, *words):
...     for word in words:
...         i = string.find(word)
...         if i < 0:
...             return False
...         string = string[i len(word):]
...     return True
...
>>> find_words(string, "blue", "bird")
True
>>> find_words(string, "bird", "blue")
False

CodePudding user response:

Nope, but I think you can roll your own. Something like this:

inps = [
    'this is blue',
    'this was blue',
    'XYZ this XYZ blue XYZ',
    'this is',
    'blue here',
]


def find_it(string: str, *werds):
    num_werds = len(werds)
    werd_idx = 0
    cur_werd = werds[werd_idx]

    for w in string.split(' '):
        if w == cur_werd:
            werd_idx  = 1
            if werd_idx == num_werds:
                return True
            cur_werd = werds[werd_idx]

    return False


for s in inps:
    print(find_it(s, 'this', 'blue'))

Out:

True
True
True
False
False

CodePudding user response:

Try this, Only with simple python code.

def check(string,patt):
    string = string.split(' ')
    patt = patt.split(' ')
    if len(string) != len(patt):
        return False    
    return all(True if v =="*" else v==string[i] for i,v in enumerate(patt))
       
        
    

patt = "* blue * sky *"
string="Hii blue is sky h"

if check(string,patt):
    print("Yes")
else:
    print("No")
  • Related