当包含新行时,模式不匹配

时间:2022-08-04 17:00:12

Here is my code

这是我的代码

 Pattern pbold = Pattern.compile(".*\\* *(.*?) *\\*.*");
 Matcher mbold = pbold.matcher(s);
 mbold.find();

3 个解决方案

#1


1  

What you need is the metacharacter that matches whitespaces charaters: (?s) This whitespace metacharacter matches:

你需要的是匹配空格字符的元字符:(?s)这个空格元字符匹配:

  • A space character
  • 空间角色

  • A tab character
  • 标签字符

  • A carriage return character
  • 回车符

  • A new line character
  • 一个新的线条字符

  • A vertical tab character
  • 垂直制表符

For more info about this special characters, please consult The Java Tutorials - Regular Expressions - Predefined Character Classes.

有关此特殊字符的更多信息,请参阅The Java Tutorials - Regular Expressions - Predefined Character Classes。

The code belows matches the case you need:

以下代码与您需要的案例相符:

String s = "abc021\n" +
            "34-+\n" +
            "*\n" +
            "a\n" +
            "p\n" +
            "p\n" +
            "l\n" +
            "e\n" +
            "*\n" +
            "fga32\n" +
            "49";
    Pattern pbold = Pattern.compile(".*\\* *((?s).*?) *\\*.*");
    Matcher mbold = pbold.matcher(s);
    mbold.find();

There is also a similar question here: Regular expression does not match newline obtained from Formatter object

这里也有类似的问题:正则表达式与从Formatter对象获得的换行符不匹配

#2


0  

Use flags igm like below:

使用标志igm如下:

Pattern pbold = Pattern.compile(".*\\* *(.*?) *\\*.*");
 Matcher mbold = pbold.matcher(s, Pattern.MULTILINE|Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
 mbold.find();

#3


0  

This regular expression might solve your problem...

这个正则表达式可以解决你的问题......

 Pattern pbold = Pattern.compile(".*\\*[ \n]*(.*?)[ \n]*\\*.*");
 Matcher mbold = pbold.matcher(s);
 mbold.find();

If this doesn't solve it..please elaborate what you are trying to get through this expression.

如果这不能解决它...请详细说明您试图通过此表达式获得的内容。

#1


1  

What you need is the metacharacter that matches whitespaces charaters: (?s) This whitespace metacharacter matches:

你需要的是匹配空格字符的元字符:(?s)这个空格元字符匹配:

  • A space character
  • 空间角色

  • A tab character
  • 标签字符

  • A carriage return character
  • 回车符

  • A new line character
  • 一个新的线条字符

  • A vertical tab character
  • 垂直制表符

For more info about this special characters, please consult The Java Tutorials - Regular Expressions - Predefined Character Classes.

有关此特殊字符的更多信息,请参阅The Java Tutorials - Regular Expressions - Predefined Character Classes。

The code belows matches the case you need:

以下代码与您需要的案例相符:

String s = "abc021\n" +
            "34-+\n" +
            "*\n" +
            "a\n" +
            "p\n" +
            "p\n" +
            "l\n" +
            "e\n" +
            "*\n" +
            "fga32\n" +
            "49";
    Pattern pbold = Pattern.compile(".*\\* *((?s).*?) *\\*.*");
    Matcher mbold = pbold.matcher(s);
    mbold.find();

There is also a similar question here: Regular expression does not match newline obtained from Formatter object

这里也有类似的问题:正则表达式与从Formatter对象获得的换行符不匹配

#2


0  

Use flags igm like below:

使用标志igm如下:

Pattern pbold = Pattern.compile(".*\\* *(.*?) *\\*.*");
 Matcher mbold = pbold.matcher(s, Pattern.MULTILINE|Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
 mbold.find();

#3


0  

This regular expression might solve your problem...

这个正则表达式可以解决你的问题......

 Pattern pbold = Pattern.compile(".*\\*[ \n]*(.*?)[ \n]*\\*.*");
 Matcher mbold = pbold.matcher(s);
 mbold.find();

If this doesn't solve it..please elaborate what you are trying to get through this expression.

如果这不能解决它...请详细说明您试图通过此表达式获得的内容。