Selenium Python如何使用regex在字符串值中查找两位数

时间:2021-09-26 18:26:55

I have a list of elements. Each element in the list has a string value in the following format:

我有一个元素列表。列表中的每个元素都有如下格式的字符串值:

Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(87)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]

I would like to extract only the numbers. The numbers always comes in brackets after Name[wi, It is always the same format.

我只想提取数字。数字总是在名字后面的括号中[wi],格式总是一样的。

How can i extract the numbers and store it in a variable so i can use it?

如何提取数字并将其存储在一个变量中以便使用?

My Python code will iterate over the elements and if it finds a number above 86 it should return false, else return true.

我的Python代码将遍历元素,如果它找到一个大于86的数字,它应该返回false,否则返回true。

My current Python code is:

我目前的Python代码是:

def is_match_audit_code_displayed(self):
elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
for i in elements:
    if "86" not in i.text:
        print i.text
        return True
    return False

Thanks, Riaz

谢谢,Riaz

Using alecxe answer I have included it in my Python method. In the list of elements find the numeric value inside the brackets using regex. For each item in the list check if the number value is less than 86. If it is return true else return false.

Here is the full method routine:

这里是完整的方法例程:

def is_match_audit_code_less_than_max_value_displayed_for_the_filter_report_results(self, max_code): 
    try:
        elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
        pattern = re.compile(r"Name\[wi ,\((\d+)\)\]")

        for element in elements:

            value = pattern.findall(element.text)
            if len(value) != 1:
                return False
            value = int(value[0])
            if value > max_code: # e.g. max_code is 86
                return False
            return True
    except NoSuchElementException, e:
        print value
        print "Element not found "
        print e
        screenshot_name = elements + value + get_datetime_now()
        self.save_screenshot(screenshot_name)

2 个解决方案

#1


2  

You can use regular expressions:

你可以使用正则表达式:

import re

elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
pattern = re.compile(r"Name\[wi ,\((\d+)\)\]")

for element in elements:
    print(pattern.findall(element.text))

In the Name\[wi ,\((\d+)\)\] expression we have to escape the [, ], ( and ) since these characters have a special meaning in regular expressions. The (\d+) part is a capturing group that would extract one or more digits.

在名称\[wi,\(\d+)\]表达式中,我们必须转义[,]和(and),因为这些字符在正则表达式中有特殊的意义。(\d+)部分是一个捕获组,可以提取一个或多个数字。

#2


0  

Use the following approach:

使用以下的方法:

import re

string = """
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(87)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
"""
# look for a digit in square brackets only
# more precise: match an opening bracket, 
# anything that is not a closing bracket, 
# digits greedily (\d+),
# anything not a closing bracket lazily 
# and a closing bracket
rx = r'\[[^]]*?(\d+)[^]]*?\]'

for match in re.finditer(rx, string):
    num = match.group(1)
    print num

See a demo on regex101.com. This approach makes sure only to find the digits in square brackets (otherwise you could have simply come up with \d+).

请参阅regex101.com上的演示。这种方法确保只找到方括号中的数字(否则您可能只想到了\d+)。

#1


2  

You can use regular expressions:

你可以使用正则表达式:

import re

elements = self.driver.find_elements_by_xpath('//table[@id="reporting_view_report_dg_main_body"]//tr//td[4]//span')
pattern = re.compile(r"Name\[wi ,\((\d+)\)\]")

for element in elements:
    print(pattern.findall(element.text))

In the Name\[wi ,\((\d+)\)\] expression we have to escape the [, ], ( and ) since these characters have a special meaning in regular expressions. The (\d+) part is a capturing group that would extract one or more digits.

在名称\[wi,\(\d+)\]表达式中,我们必须转义[,]和(and),因为这些字符在正则表达式中有特殊的意义。(\d+)部分是一个捕获组,可以提取一个或多个数字。

#2


0  

Use the following approach:

使用以下的方法:

import re

string = """
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(87)]Address[I]DOB[]Phone[]ACVSEQ[]
Name[wi ,(86)]Address[I]DOB[]Phone[]ACVSEQ[]
"""
# look for a digit in square brackets only
# more precise: match an opening bracket, 
# anything that is not a closing bracket, 
# digits greedily (\d+),
# anything not a closing bracket lazily 
# and a closing bracket
rx = r'\[[^]]*?(\d+)[^]]*?\]'

for match in re.finditer(rx, string):
    num = match.group(1)
    print num

See a demo on regex101.com. This approach makes sure only to find the digits in square brackets (otherwise you could have simply come up with \d+).

请参阅regex101.com上的演示。这种方法确保只找到方括号中的数字(否则您可能只想到了\d+)。