在Java中使用正则表达式匹配^和$

时间:2015-01-28 13:38:10

标签: java regex

我正在使用正则表达式,我遇到了^ xxx和xxx $意思是在行的开头匹配xxx正则表达式并且在行的末尾匹配正则表达式xxx。任何人都可以用示例解释这个,我很难时间了解这个

6 个答案:

答案 0 :(得分:2)

这一切都归结为你用来匹配的方法。

如果你使用String.matches(),它会考虑整个字符串,因此指定^$是没用的。

如果您使用PatternMatcher进行匹配,则可以使用^$,如下所示:

示例:1

public static void main(String[] args) {
    String s = "abcabc";
    Pattern p = Pattern.compile("abc");
    Matcher m = p.matcher(s);
    System.out.println(m.find());
    System.out.println(m.start()); // prints starting index of match
}

O / P:

true
0

示例:2

public static void main(String[] args) {
    String s = "abcabc";
    Pattern p = Pattern.compile("abc$");
    Matcher m = p.matcher(s);
    System.out.println(m.find());
    System.out.println(m.start()); // prints starting index of match
}

O / P:

true
3

示例-3:

public static void main(String[] args) {
    String s = "xxabc\nabc";
    Pattern p = Pattern.compile("^abc");
    Matcher m = p.matcher(s);
    System.out.println(m.find());
    System.out.println(m.start());
}

false
Exception in thread "main" java.lang.IllegalStateException: No match available
    at java.util.regex.Matcher.start(Unknown Source)
    at Test.main(Test.java:10)  --> OOPs.. Not at the beginning?

答案 1 :(得分:2)

这是有道理的,如果你要查找字符串的某些部分,就像在这个搜索和替换示例中一样:

System.out.println("test test test".replaceAll("^test", "TEST"));
System.out.println("test test test".replaceAll("test$", "TEST"));

输出

TEST test test
test test TEST

但如果您尝试匹配整个字符串,则会自动插入^$

System.out.println("test test test".matches("test"));
System.out.println("test test test".matches("^test$")); // The same

并且都返回false

答案 2 :(得分:1)

^xxx匹配以xxx

开头的所有字符串
  • xxx1234 - >匹配
  • xxxABDcx - >匹配
  • xxassas - >没有比赛

xxx$将所有字​​符串与xxx

匹配
  • 1234xxx - >匹配
  • Acdsadsxx - >没有比赛

答案 3 :(得分:1)

<强>含义

  • ^ - 文档/字符串的开头。
  • $ - 文档/字符串的结尾。

简要示例

  • &#34;找到作者有令人讨厌的习惯在最后输入回车的文件&#34;

\n$

Pattern pattern = Pattern.compile("\n$");
String string1 = "This is a document.";
String string2 = "This is a document.\n";
Matcher matcher1 = pattern.matcher(string1);
Matcher matcher2 = pattern.matcher(string2);
System.out.println(matcher1.find());// false
System.out.println(matcher2.find());// true
  • &#34;查找以大写字母开头的文档&#34;

^[A-Z]

Pattern pattern = Pattern.compile("^[A-Z]");
String string1 = "This is a document.";
String string2 = "this is a document.";
Matcher matcher1 = pattern.matcher(string1);
Matcher matcher2 = pattern.matcher(string2);
System.out.println(matcher1.find());// true
System.out.println(matcher2.find());// false

答案 4 :(得分:1)

一个简单的例子是:

System.out.println("abc".replaceAll("abc", "xxx")); 
System.out.println("abc".replaceAll("bc", "xx")); 
System.out.println("abc".replaceAll("^bc", "xx")); 
System.out.println("abc".replaceAll("bc$", "xx")); 
System.out.println("abc".replaceAll("^ab", "xx")); 
System.out.println("abc".replaceAll("ab$", "xx"));

输出:

xxx
axx
abc
axx
xxc
abc

匹配字符时x表示的位置。

答案 5 :(得分:1)

此代码:

if (Pattern.matches("^123.*", "123blablabla")) {
  System.out.println("It matches!");
}
else {
  System.out.println("It does not match!");
}

将输出:

  

匹配!

因为字符串&#34; 123blablabla&#34;以&#34; 123&#34;开头然后是零个或多个字符(&#34;。*&#34;在正则表达式语言中)。

以类似的方式,以下代码:

if (Pattern.matches(".*abc$", "some string that ends with abc")) {
  System.out.println("It matches!");
}
else {
  System.out.println("It does not match!");
}

将输出

  

匹配!

因为字符串&#34;某些以abc&#34结尾的字符串;结束于&#34; abc&#34;。

相关问题