Generic
Если вы хотите создать общее выражение, возможно, вы можете начать с некоторого выражения, похожего на
\S*the[^o\s]*\b
, в зависимости от того, что вы хотите сопоставить и не сопоставить, Я полагаю.
Неуниверсальный
Я думаю, вы можете просто найти границы слов (\b
), полезные для решения вашей проблемы, с некоторыми простымивыражение, подобное,
\b[Tt]he\b|\b[Tt]hen\b|\bextratheaterly\b
Или,
\b(?:[Tt]hen?|[Ee]xtratheaterly)\b
Java Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpression{
public static void main(String[] args){
final String regex = "\\b(?:[Tt]hen?|[Ee]xtratheaterly)\\b";
final String string = "If the world says that theo is not oreo cookies then thetatheoder is extratheaterly good.\n\n"
+ "If The world says that theo is not oreo cookies Then thetatheoder is Extratheaterly good.\n\n"
+ "If notthe world says that theo is not oreo cookies notthen thetatheoder is notextratheaterly good.\n\n\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Выход
Full match: the
Full match: then
Full match: extratheaterly
Full match: The
Full match: Then
Full match: Extratheaterly
Python Test
import re
string = '''
If the world says that theo is not oreo cookies then thetatheoder is extratheaterly good.
If The world says that theo is not oreo cookies Then thetatheoder is Extratheaterly good.
If notthe world says that theo is not oreo cookies notthen thetatheoder is notextratheaterly good.
'''
expression = r'\b(?:[Tt]hen?|[Ee]xtratheaterly)\b'
print(re.findall(expression, string))
print([m.group(0) for m in re.finditer(expression, string)])
Выход
['the', 'then', 'extratheaterly', 'The', 'Then', 'Extratheaterly']
['the', 'then', 'extratheaterly', 'The', 'Then', 'Extratheaterly']
Если вы хотитечтобы упростить / изменить / изучить выражение, это было объяснено на верхней правой панели regex101.com . При желании вы также можете посмотреть в эту ссылку , как она будет сопоставляться с некоторыми примерами входных данных.
RegEx Circuit
jex.im визуализирует регулярные выражения: