繁体   English   中英

使用正则表达式忽略子字符串

[英]using regular expressions to ignore a substring

我正在使用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

试试吧:

输入:

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);
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM