Java正则表达式递归地获取特定单词前后的11个单词
我试图在字符串中的特定单词前后获得11个单词 例如:Java正则表达式递归地获取特定单词前后的11个单词,java,regex,Java,Regex,我试图在字符串中的特定单词前后获得11个单词 例如: and WINSOCK 2.0 in Visual Studio 2012/2013, compiled as Release for use on 64-bit and 32-bit Windows Servers. Client application discovers and validates qualifying Windows Server product 现在的挑战是识别像32这样的单词,它通过hyhen连接到单词位。如果我
and WINSOCK 2.0 in Visual Studio 2012/2013, compiled as Release for use on 64-bit and 32-bit Windows Servers. Client application discovers and validates qualifying Windows Server product
现在的挑战是识别像32这样的单词,它通过hyhen连接到单词位。如果我将这个单词改为32+位而不是32位。。。正则表达式识别并获取句子前后的11个单词
我的正则表达式看起来像
Pattern pattern = Pattern.compile("(?<!-)\\b(?<!&)(" + "\\b" + word + "\\b" + ")(?!&)\\b(?!-)(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,11}");
Pattern=Pattern.compile((?如果您不必使用正则表达式,那么:
String message = "and WINSOCK 2.0 in Visual Studio 2012/2013, compiled as Release for use on 64-bit and 32-bit Windows Servers. Client application discovers and validates qualifying Windows Server product";
String target = "32-bit";
int index = message.indexOf("32-bit");
int lenght = target.length();
String before = message.substring(index -11, index);
String after = message.substring(index + lenght , index + lenght + 11);
Log.i("tag", "index: " + index);
Log.i("tag", "before: " + before);
Log.i("tag", "after: " + after);
您可以从正则表达式中“取出”连字符:
"\\b(?<!&)" + word + "\\b(?!&)(?:[^a-zA-Z']+[a-zA-Z'-]+){0,11}"
“\\b(?那么你想把32位识别为1个字还是2个字?我只想识别32个字。模式应该匹配。我不需要完整的{32位}。谢谢32位
应该识别为32
和位
?那么为什么要包含连字符呢?”\\b(?@Nick我需要从任何字符串中获取word 32的上下文并突出显示..现在在我的例子中..由于字符串中存在32,因此应该匹配它,并且将突出显示应用程序级别(仅突出显示word 32)…我添加了一个正则表达式的变体,如果你的单词
可能以一个特殊的正则表达式元字符开头/结尾(比如(
,[
,+
,等等)@WiktorStribiż你的解释对我有很大帮助。我希望它能对像我这样完全不了解正则表达式的人有所帮助。
"\\b(?<!&)" + word + "\\b(?!&)(?:[^a-zA-Z']+[a-zA-Z'-]+){0,11}"
"(?<![&\\w])" + Pattern.quote(word) + "(?![&\\w])(?:[^a-zA-Z']+[a-zA-Z'-]+){0,11}"