Roelof Coertze
Roelof Coertze

Reputation: 586

Matching number as string at beginning of string while passing variable in python

Stupid question, but I am at a loss for trying! I am trying to find matches in array with this number, passed as string, but only from the start of the other string.

Below is an example of what I am trying to do. In the comment section I am trying to pass the query string as a variable, but not sure how to do this.

Below this the query is manually added and should print True for array[1] but I am getting no positive result. The number should only match strings where they match at the beginning followed by any other combinations of 1's and 2's.

t1 = "112"

array = [['121121'], ['1121211']]

#for item in array:
#    if re.search(r'^t1.', str(item)):
#        print(item)
#        print(True)

for item in array:
    if re.search(r'^112.', str(item)):
        print(item)
        print(True)

Upvotes: 1

Views: 181

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627507

The problem is due to the fact that you type-cast the list to a string. You need re.search(r'^112.', item[0]): or similar to get to the first list item string.

Also, . matches any char but a line break char, but you mention "The number should only match strings where they match at the beginning followed by any other combinations of 1's and 2's.", so the . must be replaced with [12]*. Together with re.fullmatch, this will fix the issue.

You can use

import re
t1 = "112"
array = [['121121'], ['1121211']]
for item in array:
    if item and re.fullmatch(fr'{t1}[12]*', item[0]):
        print(True,'->', item)
    else:
        print(False,'->', item)

See the Python demo. Output:

False -> ['121121']
True -> ['1121211']

NOTES:

  • fr'{t1}[12]*' builds the regex dynamically by passing t1 to the pattern, and
  • re.fullmatch allows entire string match only.
  • if item: makes sure the regex is run only on non-empty items.

A non-regex approach:

t1 = "112"
array = [['121121'], ['1121211'], ['112223']]
for item in array:
    if item and item[0].startswith(t1) and all(x in ['1','2'] for x in item[0]):
        print(True,'->', item)
    else:
        print(False,'->', item)

See this Python demo.

Upvotes: 2

John Coleman
John Coleman

Reputation: 52008

The simplest solution here is to ditch re and use the startswith string method:

[row[0].startswith(t1) for row in array]

evaluates to

[False, True]

Upvotes: 0

Jakob Lindskog
Jakob Lindskog

Reputation: 130

array is a nested list, so item is equal to ['121121'] on the first iteration and ['1121211'] on the second.

You probably meant to declare array as array = ['121121','1121211']:

t1 = "112"

array = ['121121','1121211']

#for item in array:
#    if re.search(r'^t1.', str(item)):
#        print(item)
#        print(True)

for item in array:
    if re.search(r'^112.', str(item)):
        print(item)
        print(True)

I get the following output:

1121211
True

Passing a variable is simple:

t1 = "112"

array = ['121121','1121211']

for item in array:
    if re.search(f'^{t1}', str(item)):
        print(item)
        print(True)

Upvotes: 0

Related Questions