strings - Regular Expressions - Basics

In Java, \\ means "I'm inserting a regular expression backslash, so the following character has special meaning." To insert a literal backslash, we say \\\.

// strings/IntegerMatch.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class IntegerMatch {
  public static void main(String[] args) {
    System.out.println("-1234".matches("-?\\d+"));
    System.out.println("5678".matches("-?\\d+"));
    System.out.println("+911".matches("-?\\d+"));
    System.out.println("+911".matches("(-|\\+)?\\d+"));
  }
}
/* Output:
true
true
false
true
*/
// strings/Splitting.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

import java.util.*;

public class Splitting {
  public static String knights =
      "Then, when you have found the shrubbery, "
          + "you must cut down the mightiest tree in the "
          + "forest...with... a herring!";

  public static void split(String regex) {
    System.out.println(Arrays.toString(knights.split(regex)));
  }

  public static void main(String[] args) {
    split(" "); // Doesn't have to contain regex chars
    split("\\W+"); // Non-word characters
    split("n\\W+"); // 'n' followed by non-words
  }
}
/* Output:
[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest...with..., a, herring!]
[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest...with... a herring!]
*/

With regular expression replacement, we can either replace the first occurrence, or all of them:

// strings/Replacing.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class Replacing {
  static String s = Splitting.knights;

  public static void main(String[] args) {
    System.out.println(s.replaceFirst("f\\w+", "located"));
    System.out.println(s.replaceAll("shrubbery|tree|herring", "banana"));
    System.out.println("forest...".matches("f\\w+"));
    System.out.println("forest".matches("f\\w+"));
  }
}
/* My Output:
Then, when you have located the shrubbery, you must cut down the mightiest tree in the forest...with... a herring!
Then, when you have found the banana, you must cut down the mightiest banana in the forest...with... a banana!
false
true
*/
POSIXNon-standardPerl/TclVimJavaASCIIDescription
 [:ascii:][29]  \p{ASCII}[\x00-\x7F]ASCII characters
[:alnum:]   \p{Alnum}[A-Za-z0-9]Alphanumeric characters
 [:word:][29]\w\w\w[A-Za-z0-9_]Alphanumeric characters plus "_"
  \W\W\W[^A-Za-z0-9_]Non-word characters
[:alpha:]  \a\p{Alpha}[A-Za-z]Alphabetic characters
[:blank:]  \s\p{Blank}[ \t]Space and tab
  \b\< \>\b(?<=\W)(?=\w)|(?<=\w)(?=\W)Word boundaries
    \B(?<=\W)(?=\W)|(?<=\w)(?=\w)Non-word boundaries
[:cntrl:]   \p{Cntrl}[\x00-\x1F\x7F]Control characters
[:digit:] \d\d\p{Digit} or \d[0-9]Digits
  \D\D\D[^0-9]Non-digits
[:graph:]   \p{Graph}[\x21-\x7E]Visible characters
[:lower:]  \l\p{Lower}[a-z]Lowercase letters
[:print:]  \p\p{Print}[\x20-\x7E]Visible characters and the space character
[:punct:]   \p{Punct}[][!"#$%&'()*+,./:;<=>?@\^_`{|}~-]Punctuation characters
[:space:] \s\_s\p{Space} or \s\t\r\n\v\f]Whitespace characters
  \S\S\S[^ \t\r\n\v\f]Non-whitespace characters
[:upper:]  \u\p{Upper}[A-Z]Uppercase letters
[:xdigit:]  \x\p{XDigit}[A-Fa-f0-9]Hexadecimal digits

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/IntegerMatch.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/Splitting.java

4.https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/Replacing.java

5. https://en.wikipedia.org/wiki/Regular_expression

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值