java - 正则表达式匹配具有固定前缀和后缀的可变数量的作品

标签 java regex

示例输入: xxx xxx xxx(可以是任意数量的单词)是活着还是死了?

例如:

 Did   Michael      Jackson  live or     die     ?

我想捕捉:迈克尔· jackson ,生,死。句子中的单词之间可以有任意数量的空格。

我该怎么做?

最佳答案

像这样的东西会起作用。您需要选择第一组 Michael Jackson,并将其按空格字符分开。

Pattern regex = Pattern.compile("^Did (.+)\s+(\w+)\s+or\s+(\w+)$", 
                                  Pattern.CASE_INSENSITIVE | 
                                  Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) 
{
  String []person = regexMatcher.group(0).split(" ");
  String action1 = regexMatcher.group(1);
  String action2 = regexMatcher.group(2);
} 

关于java - 正则表达式匹配具有固定前缀和后缀的可变数量的作品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1480364/

相关文章:

java - 有没有人想出如何使 javafx tableview 像 jtable 一样工作?

java - 从单独的 Java/Swing 进程嵌入 Swing 组件

java - 无法在项目 aopencommon 上执行目标 org.apache.maven.plugins :maven-compiler-plugin:2. 3.2:compile (default-compile)

Java检测字符串中的特殊字符

java - 无法在java中使用此正则表达式提取字符串

java - 线程 “main”中的异常java.lang.NegativeArraySizeException

java - 无法在 Mac 上安装 Eclipse - "Failed to create the Java Virtual Machine"

Javascript 正则表达式错误,带有 'm' 标志,当正则表达式有效且在其他地方工作时,错误为 'invalid regexp group'

c# - XSD :Pattern and C# Regex?之间有区别吗

javascript - 正则表达式关键字过滤