从字符串 python re 库中提取子字符串
Extract substring from string python re library
我有一个字符串
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'
使用正则表达式 python 我想提取类型。在这种情况下我想要的输出是 2-A.
我试过是
import re
type = re.findall(r'Type: \d*-', string)
print(type)
我有多个这种类型的字符串,我想提取 'Type:' 和“|”之间的代码文本。
如果 Type
只包含一个数字“-”和一个字母
,这应该会为您提供所需的结果
import re
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'
type_str = re.search('(Type:\s\d+-\w+)', string).group()
print(type_str)
Type: 2-A
或者如果您只想提取 2-A
import re
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'
type_str = re.search('(Type:\s\d-\w)', string).group()
print(type_str.split(': ')[1])
2-A
最后根据要求提取从 Type:
到 |
的任何文本,它将是
import re
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 10 X-ASFD 34 10 | S-no. : dfwef | Name : dfwf'
type_str = re.search('Type:\s(.*?\|)', string).group()
print(type_str.split(': ')[1].replace('|',''))
10 X-ASFD 34 10
使用正则表达式'(?<=Type: )[\w-]+'
(?<=Type: )
将在 类型之后提取所有内容:
[\w-]+
将仅提取 个数字 、 个单词 和 -
import re
re.findall(r'(?<=Type: )[\w-]+',string)
>> ['2-A']
我有一个字符串
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'
使用正则表达式 python 我想提取类型。在这种情况下我想要的输出是 2-A.
我试过是
import re
type = re.findall(r'Type: \d*-', string)
print(type)
我有多个这种类型的字符串,我想提取 'Type:' 和“|”之间的代码文本。
如果 Type
只包含一个数字“-”和一个字母
import re
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'
type_str = re.search('(Type:\s\d+-\w+)', string).group()
print(type_str)
Type: 2-A
或者如果您只想提取 2-A
import re
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'
type_str = re.search('(Type:\s\d-\w)', string).group()
print(type_str.split(': ')[1])
2-A
最后根据要求提取从 Type:
到 |
的任何文本,它将是
import re
string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 10 X-ASFD 34 10 | S-no. : dfwef | Name : dfwf'
type_str = re.search('Type:\s(.*?\|)', string).group()
print(type_str.split(': ')[1].replace('|',''))
10 X-ASFD 34 10
使用正则表达式'(?<=Type: )[\w-]+'
(?<=Type: )
将在 类型之后提取所有内容:[\w-]+
将仅提取 个数字 、 个单词 和 -
import re
re.findall(r'(?<=Type: )[\w-]+',string)
>> ['2-A']