假设我有一个字符串'gfgfdAAA1234ZZZuijjk',我想提取'1234'部分。
我只知道在AAA之前的几个字符,以及在ZZZ之后的我感兴趣的部分1234。
使用sed,可以对字符串执行如下操作:
echo "$STRING" | sed -e "s|.*AAA\(.*\)ZZZ.*|\1|"
结果是1234。
如何在Python中做同样的事情?
假设我有一个字符串'gfgfdAAA1234ZZZuijjk',我想提取'1234'部分。
我只知道在AAA之前的几个字符,以及在ZZZ之后的我感兴趣的部分1234。
使用sed,可以对字符串执行如下操作:
echo "$STRING" | sed -e "s|.*AAA\(.*\)ZZZ.*|\1|"
结果是1234。
如何在Python中做同样的事情?
当前回答
打印稿。获取两个字符串之间的字符串。
搜索前缀和后缀之间的最短字符串
前缀-字符串/字符串数组/ null(意味着从开始搜索)。
Postfixes -字符串/字符串数组/ null(意味着搜索直到结束)。
public getStringInBetween(str: string, prefixes: string | string[] | null,
postfixes: string | string[] | null): string {
if (typeof prefixes === 'string') {
prefixes = [prefixes];
}
if (typeof postfixes === 'string') {
postfixes = [postfixes];
}
if (!str || str.length < 1) {
throw new Error(str + ' should contain ' + prefixes);
}
let start = prefixes === null ? { pos: 0, sub: '' } : this.indexOf(str, prefixes);
const end = postfixes === null ? { pos: str.length, sub: '' } : this.indexOf(str, postfixes, start.pos + start.sub.length);
let value = str.substring(start.pos + start.sub.length, end.pos);
if (!value || value.length < 1) {
throw new Error(str + ' should contain string in between ' + prefixes + ' and ' + postfixes);
}
while (true) {
try {
start = this.indexOf(value, prefixes);
} catch (e) {
break;
}
value = value.substring(start.pos + start.sub.length);
if (!value || value.length < 1) {
throw new Error(str + ' should contain string in between ' + prefixes + ' and ' + postfixes);
}
}
return value;
}
其他回答
使用正则表达式-供进一步参考的文档
import re
text = 'gfgfdAAA1234ZZZuijjk'
m = re.search('AAA(.+?)ZZZ', text)
if m:
found = m.group(1)
# found: 1234
or:
import re
text = 'gfgfdAAA1234ZZZuijjk'
try:
found = re.search('AAA(.+?)ZZZ', text).group(1)
except AttributeError:
# AAA, ZZZ not found in the original string
found = '' # apply your error handling
# found: 1234
这里有一个没有regex的解决方案,它也适用于第一个子字符串包含第二个子字符串的场景。如果第二个标记在第一个标记之后,此函数将只查找子字符串。
def find_substring(string, start, end):
len_until_end_of_first_match = string.find(start) + len(start)
after_start = string[len_until_end_of_first_match:]
return string[string.find(start) + len(start):len_until_end_of_first_match + after_start.find(end)]
正则表达式
import re
re.search(r"(?<=AAA).*?(?=ZZZ)", your_text).group(0)
如果your_text中没有“AAA”和“ZZZ”,上述as-is将失败,并出现AttributeError
字符串的方法
your_text.partition("AAA")[2].partition("ZZZ")[0]
如果your_text中不存在"AAA"或"ZZZ",上述函数将返回空字符串。
PS Python挑战?
>>> s = '/tmp/10508.constantstring'
>>> s.split('/tmp/')[1].split('constantstring')[0].strip('.')
打印稿。获取两个字符串之间的字符串。
搜索前缀和后缀之间的最短字符串
前缀-字符串/字符串数组/ null(意味着从开始搜索)。
Postfixes -字符串/字符串数组/ null(意味着搜索直到结束)。
public getStringInBetween(str: string, prefixes: string | string[] | null,
postfixes: string | string[] | null): string {
if (typeof prefixes === 'string') {
prefixes = [prefixes];
}
if (typeof postfixes === 'string') {
postfixes = [postfixes];
}
if (!str || str.length < 1) {
throw new Error(str + ' should contain ' + prefixes);
}
let start = prefixes === null ? { pos: 0, sub: '' } : this.indexOf(str, prefixes);
const end = postfixes === null ? { pos: str.length, sub: '' } : this.indexOf(str, postfixes, start.pos + start.sub.length);
let value = str.substring(start.pos + start.sub.length, end.pos);
if (!value || value.length < 1) {
throw new Error(str + ' should contain string in between ' + prefixes + ' and ' + postfixes);
}
while (true) {
try {
start = this.indexOf(value, prefixes);
} catch (e) {
break;
}
value = value.substring(start.pos + start.sub.length);
if (!value || value.length < 1) {
throw new Error(str + ' should contain string in between ' + prefixes + ' and ' + postfixes);
}
}
return value;
}