サブ

2017-12-19 6 views
0

を無視するように正規表現を使用して、私は、Javaを使用していますが、私はこれを一致させるために、正規表現を使用したいI am going to manchester のようなパターンタイプI am going(一部の文字列)to manchesterでありますすべての文章を持っています。 だから私の文字列i am going to manchesterも一致しますI am going with my family to manchesterサブ

これを達成するための正規表現を知っていますか?

私は次のことを試してみましたが、それは動作しません。

Pattern.compile("i am going (\\w+) to mancheter") 

私はPatternMatcherを使用しています。

+0

'私は行くよ(+)?マンチェスターへ」または単に「私はマンチェスターへ\ b。*行くつもりですか? – ctwheels

+0

['私は行くつもりですか?マンチェスター\ .'](https://regex101.com/r/Ej9A5j/1)しかし、これはお勧めできません。 – Jan

+0

「私はマンチェスターに行く」という文字列も拒否する必要がありますか? – AntonH

答えて

0

それを試してみてください。

入力:

I am going with my family to manchester potato 

出力:

I am going with my family to manchester 

Javaコード:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

// one class needs to have a main() method 
public class HelloWorld { 
// arguments are passed using the text field below this editor 
public static void main(String[] args) { 

    final String regex = "(<?I am going)(.+)(?>manchester)"; 
    final String string = "I am going with my family to manchester patata"; 

    final Pattern pattern = Pattern.compile(regex); 
    final Matcher matcher = pattern.matcher(string); 
    String answer = ""; 

    while (matcher.find()) { 
    answer = matcher.group(0) + ""; 
    } 


    System.out.println(answer); 
} 
}