假设我有一个字符串'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;
}
其他回答
以防有人要做和我一样的事。我必须在一行中提取圆括号内的所有内容。例如,如果我有这样一句话,‘美国总统(巴拉克·奥巴马)会见了……,我只想得到“巴拉克·奥巴马”,这是解决方案:
regex = '.*\((.*?)\).*'
matches = re.search(regex, line)
line = matches.group(1) + '\n'
也就是说,你需要用斜杠\符号来阻止括号。尽管这是一个关于更多正则表达式的问题。
此外,在某些情况下,你可能会在正则表达式定义之前看到'r'符号。如果没有r前缀,你需要像在c中那样使用转义字符。这里有更多关于这个的讨论。
打印稿。获取两个字符串之间的字符串。
搜索前缀和后缀之间的最短字符串
前缀-字符串/字符串数组/ 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;
}
在python中,可以使用正则表达式(re)模块中的findall方法从字符串中提取子字符串。
>>> import re
>>> s = 'gfgfdAAA1234ZZZuijjk'
>>> ss = re.findall('AAA(.+)ZZZ', s)
>>> print ss
['1234']
>>> s = '/tmp/10508.constantstring'
>>> s.split('/tmp/')[1].split('constantstring')[0].strip('.')
只需一行代码就可以做到
>>> import re
>>> re.findall(r'\d{1,5}','gfgfdAAA1234ZZZuijjk')
>>> ['1234']
结果将收到列表…