regexjava中的模式
我想在我的文字上找到图案的起始位置和结束位置 我使用以下代码:regexjava中的模式,java,regex,pattern-matching,Java,Regex,Pattern Matching,我想在我的文字上找到图案的起始位置和结束位置 我使用以下代码: Pattern _pat=Pattern.compile("20. Eslaminejad MB, Nikmahzar A, Piriea A. The structure of " +"Human Mesenchymal Stem Cells differentiated into cartilage in micro mass culture system. Yakhteh 2006, 8(3): " +"162-17
Pattern _pat=Pattern.compile("20. Eslaminejad MB, Nikmahzar A, Piriea A. The structure of "
+"Human Mesenchymal Stem Cells differentiated into cartilage in micro mass culture system. Yakhteh 2006, 8(3): "
+"162-171.");
Matcher _match=_pat.matcher("19. Eslaminejad MB, Eftekhari-Yazdi P. Mesenchymal stem "
+"cells: In vitro differentiation among bone and cartilage cell "
+"lineages. Yakhteh. 2007; 9(3): 158-169."
+"20. Eslaminejad MB, Nikmahzar A, Piriea A. The structure of "
+"Human Mesenchymal Stem Cells differentiated into cartilage in micro mass culture system. Yakhteh 2006, 8(3): "
+"162-171."
+"21. Pioletti DP, Montjovent MO, Zambelli PY, Applegate L. "
+"Bone tissue engineering using foetal cell therapy. Swiss "
+"Med Wkly. 2006 ; 136(35-36): 557-560.");
while(_match.find()){
System.out.println(_match.start());
System.out.println(_match.end());
}
不幸的是,该模式无法在我的文本中找到字符串。显然,您需要的不是正则表达式,而是
int start = inputString.indexOf(stringToSearch);
这将为您提供开始位置,结束位置的计算如下
int end = start+stringToSearch.length();
在尝试匹配之前,您可能需要规范化空白或执行其他典型的规范化操作。您的问题在于不同数量的空格。比较:
The structure of
从图案上看
The structure of
从文本中删除
顺便说一下,虽然您使用的是模式,但您不使用模式提供的任何功能,因此在您的情况下,您可以使用simple
String.contains()
方法 这不是使用正则表达式的方式。请查看此页面:在搜索字符串中,单词之间的空格比搜索文本中的空格多。例如,在“structure”和“of”之间,仅当不能使用普通字符串函数时才使用正则表达式。。