Java正则匹配美元表达式,Java正则表达式中的新行和美元符号

I know $ is used to check if a line end follows in a Java regular expression.

For the following codes:

String test_domain = "http://www.google.com/path\nline2\nline3";

test_domain = test_domain.replaceFirst("(\\.[^:/]+).*$?", "$1");

System.out.println(test_domain);

The output is:

http://www.google.com

line2

line3

I assume that the pattern (\\.[^:/]+).*$? matches the first line, which is http://www.google.com/path, and the $1 is http://www.google.com. The ? makes a reluctant match (so matches the first line.)

However, if I remove the ? in the pattern and implement following codes:

String test_domain = "http://www.google.com/path\nline2\nline3";

test_domain = test_domain.replaceFirst("(\\.[^:/]+).*$", "$1");

System.out.println(test_domain);

The output is:

http://www.google.com/path

line2

line3

I think it should give out the result http://www.google.com

(\\.[^:/]+) matches http://www.google.com

.*$ matches /path\nline2\nline3

Where is my misunderstanding of the regex here?

解决方案

Your regex does not match the input string.In fact, $ matches exactly the end of string (at the end of line3). Since you are not using an s flag, the . cannot get there.

More, the $ end of line/string anchor cannot have ? quantifier after it. It makes no sense for the regex engine, and is ignored in Java.

To make it work at all, you need to use s flag if you want to just return http://www.google.com:

String test_domain = "http://www.google.com/path\nline2\nline3";

test_domain = test_domain.replaceFirst("(?s)(\\.[^:/]+).*$", "$1");

System.out.println(test_domain);

Output of this demo:

http://www.google.com

With a multiline (?m) flag, the regex will process each line looking for a literal . and then a sequence of characters other than : and /. When one of these characters is found, the rest of characters on that line will be omitted.

String test_domain = "http://www.google.com/path\nline2\nline3";

test_domain = test_domain.replaceFirst("(?m)(\\.[^:/]+).*$", "$1");

System.out.println(test_domain);

http://www.google.com

line2

line3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值