在 python 中使用正则表达式针对各种情况在 'and' 连词之前插入后继词
Inserting the succeeded word before 'and' conjunction using regex in python for various cases
使用字符串示例的不同情况的问题。
案例一
输入字符串:
However, the gene of hBD-1 and LL-27 expression was not affected by acnes.
代码:
import re
str_a = "However, the gene of hBD-1 and LL-27 expression was not affected by acnes."
out_a = re.sub(r'\b(\w+-\d+)\b(?! expression\b)', r' expression', str_a)
print(out_a)
输出字符串:
However, the gene of hBD-1 expression and LL-27 expression was not affected by acnes.
案例二
输入字符串:
The gene of acne and non-acne patients was affected by cancer.
代码:
import re
str_b = "The gene of acne and non-acne patients was affected by cancer."
out_b = re.sub(r'\b(acne)\b(?! patients\b)', r' patients', str_b)
print(out_b)
输出字符串:
The gene of acne patients and non-acne patients was affected by cancer.
案例三
输入字符串:
Since, the gene of hBD-1 and LL-27 expression was not affected by acnes therefore the gene of acne and non-acne patients was affected by cancer.
预期输出字符串:
Since, the gene of hBD-1 expression and LL-27 expression was not affected by acnes therefore the gene of acne patients and non-acne patients was affected by cancer.
我需要的:
如何为一般情况制作这两个正则表达式?我必须为两个不同的字符串执行两个不同的正则表达式。在案例 3 中,我如何将这两种情况的正则表达式组合成一个正则表达式。请修改正则表达式或提供任何其他更好的解决方案。
您可能会使用 2 个捕获组并使用 sub 和 lambda 检查组。
import re
regex = r"\b(\w+-\d+)\b(?! expression\b)|\b(acne)\b(?! patients\b)"
s = ("However, the gene of hBD-1 and LL-27 expression was not affected by acnes.\n\n"
"The gene of acne and non-acne patients was affected by cancer.\n\n"
"Since, the gene of hBD-1 and LL-27 expression was not affected by acnes therefore the gene of acne and non-acne patients was affected by cancer.")
result = re.sub(regex, lambda x: x.group(1) + " expression" if x.group(1) else x.group(2) + " patients", s)
print(result)
输出
However, the gene of hBD-1 expression and LL-27 expression was not affected by acnes.
The gene of acne patients and non-acne patients was affected by cancer.
Since, the gene of hBD-1 expression and LL-27 expression was not affected by acnes therefore the gene of acne patients and non-acne patients was affected by cancer.
使用字符串示例的不同情况的问题。
案例一
输入字符串:
However, the gene of hBD-1 and LL-27 expression was not affected by acnes.
代码:
import re
str_a = "However, the gene of hBD-1 and LL-27 expression was not affected by acnes."
out_a = re.sub(r'\b(\w+-\d+)\b(?! expression\b)', r' expression', str_a)
print(out_a)
输出字符串:
However, the gene of hBD-1 expression and LL-27 expression was not affected by acnes.
案例二
输入字符串:
The gene of acne and non-acne patients was affected by cancer.
代码:
import re
str_b = "The gene of acne and non-acne patients was affected by cancer."
out_b = re.sub(r'\b(acne)\b(?! patients\b)', r' patients', str_b)
print(out_b)
输出字符串:
The gene of acne patients and non-acne patients was affected by cancer.
案例三
输入字符串:
Since, the gene of hBD-1 and LL-27 expression was not affected by acnes therefore the gene of acne and non-acne patients was affected by cancer.
预期输出字符串:
Since, the gene of hBD-1 expression and LL-27 expression was not affected by acnes therefore the gene of acne patients and non-acne patients was affected by cancer.
我需要的:
如何为一般情况制作这两个正则表达式?我必须为两个不同的字符串执行两个不同的正则表达式。在案例 3 中,我如何将这两种情况的正则表达式组合成一个正则表达式。请修改正则表达式或提供任何其他更好的解决方案。
您可能会使用 2 个捕获组并使用 sub 和 lambda 检查组。
import re
regex = r"\b(\w+-\d+)\b(?! expression\b)|\b(acne)\b(?! patients\b)"
s = ("However, the gene of hBD-1 and LL-27 expression was not affected by acnes.\n\n"
"The gene of acne and non-acne patients was affected by cancer.\n\n"
"Since, the gene of hBD-1 and LL-27 expression was not affected by acnes therefore the gene of acne and non-acne patients was affected by cancer.")
result = re.sub(regex, lambda x: x.group(1) + " expression" if x.group(1) else x.group(2) + " patients", s)
print(result)
输出
However, the gene of hBD-1 expression and LL-27 expression was not affected by acnes.
The gene of acne patients and non-acne patients was affected by cancer.
Since, the gene of hBD-1 expression and LL-27 expression was not affected by acnes therefore the gene of acne patients and non-acne patients was affected by cancer.