简体   繁体   English

从字符串 python 库中提取 substring

[英]Extract substring from string python re library

I have a string我有一个字符串

string = 'Ph no. : 999999999 | year: 2021 | class no.: 10Type: 2-A | S-no. : dfwef | Name : dfwf'

Using regular expression python I want to extract Type.使用正则表达式 python 我想提取类型。 Output I want in this case is 2-A. Output 在这种情况下我想要的是 2-A。

I have tried is我试过的是

import re
type = re.findall(r'Type: \d*-', string)
print(type)

I have multiples strings of this type and i want to extract code text between 'Type:' and '|'.我有多个这种类型的字符串,我想在“类型:”和“|”之间提取代码文本。

This should give you the needed result if Type contains only one number, '-', and a letter如果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

Or if you want to extract only the 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 2-A

And finally as requested to extract any text from Type: to |最后根据要求从Type: to |中提取任何文本it will be这将是

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 10 X-ASFD 34 10

Use regex '(?<=Type: )[\w-]+'使用正则表达式'(?<=Type: )[\w-]+'

  • (?<=Type: ) will extract everything after Type: (?<=Type: )将提取Type:之后的所有内容
  • [\w-]+ will extract only digits , words and - [\w-]+将仅提取数字单词-
import re
re.findall(r'(?<=Type: )[\w-]+',string)
>> ['2-A']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM