如何使用正则表达式匹配单词列表.
就像我想要匹配
This is a apple
This is a orange
This is a peach
Run Code Online (Sandbox Code Playgroud)
我试过了This is a [apple|range|peach].
不行.
有任何想法吗?我已经发了5个小时,发布了"规则",但没有详尽的例子,这些规则太神秘了.
您可以使用
Pattern pattern = Pattern.compile( "This is a (apple|orange|peach)" );
Matcher matcher = pattern.matcher( "This is a orange" );
if( matcher.find() ) {
System.out.println( matcher.group( 1 ) );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1457 次 |
| 最近记录: |