一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Java 簡化正則表達式的使用

Java 簡化正則表達式的使用

2020-09-21 17:06YouXianMing JAVA教程

本篇文章主要介紹了Java 簡化正則表達式使用的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧

使用

RegexString.with(string).pattern(pattern).start() + 后續操作(matches,find或者是replace)

源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com;
 
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author [email protected] 用于簡化處理正則表達式
 */
public class RegexString {
 
  private String string;
  private Pattern pattern;
  private Matcher matcher;
 
  ////////////////////// Constructor //////////////////////
 
  /**
   * 正則表達式對象
   *
   * @param str
   *      初始化用的字符串
   */
  public RegexString(String str) {
    setString(Objects.requireNonNull(str));
  }
 
  ////////////////////// Normal Method //////////////////////
 
  /**
   * 設置正則表達式的pattern
   *
   * @param regex
   *      正則表達式語句
   * @return RegexString
   */
  public RegexString pattern(String regex) {
    setPattern(Pattern.compile(regex));
    return this;
  }
 
  /**
   * 設置正則表達式的pattern
   *
   * @param regex
   *      正則表達式語句
   * @param flags
   *      正則表達式flag值
   * @return RegexString
   */
  public RegexString pattern(String regex, int flags) {
    setPattern(Pattern.compile(regex, flags));
    return this;
  }
 
  /**
   * 正則表達式對象開始匹配(設置完pattern后需要自行此語句才能做后續操作)
   *
   * @return RegexString
   */
  public RegexString start() {
    setMatcher(pattern.matcher(string));
    return this;
  }
 
  /**
   * 進行文本替換
   *
   * @param replacement
   *      用來替換的文本
   * @return 替換后的字符串
   */
  public String replace(String replacement) {
    return getMatcher().replaceAll(replacement);
  }
 
  /**
   * 判斷是否匹配(一次性匹配全部文本,不分步)
   *
   * @return 匹配了返回true,沒有匹配返回false.
   */
  public boolean matches() {
    return getMatcher().matches();
  }
 
  /**
   * 判斷是否匹配(分步匹配文本,請結合while循環使用)
   *
   * @return 找到了返回true,沒有找到返回false.
   */
  public boolean find() {
    return getMatcher().find();
  }
 
  /**
   * find()操作成功后,可以通過matchString()獲取匹配的字符串
   *
   * @return 匹配的字符串
   */
  public String matchString() {
    return getMatcher().group();
  }
 
  /**
   * find()操作成功后,可以通過matchStart()獲取匹配的起始位置
   *
   * @return 匹配的起始位置
   */
  public int matchStart() {
    return getMatcher().start();
  }
 
  /**
   * find()操作成功后,可以通過matchEnd()獲取匹配的結束位置
   *
   * @return 匹配的起始位置
   */
  public int matchEnd() {
    return getMatcher().end();
  }
 
  ////////////////////// Static Method //////////////////////
 
  /**
   * [靜態方法] 便利構造器
   *
   * @param str
   *      初始化用的字符串
   * @return RegexString
   */
  public static RegexString with(String str) {
    return new RegexString(str);
  }
 
  ////////////////////// Getter & Setter //////////////////////
 
  public String getString() {
    return string;
  }
 
  public void setString(String string) {
    this.string = string;
  }
 
  public Pattern getPattern() {
    return pattern;
  }
 
  public void setPattern(Pattern pattern) {
    this.pattern = pattern;
  }
 
  public Matcher getMatcher() {
    return matcher;
  }
  public void setMatcher(Matcher matcher) {
    this.matcher = matcher;
  }
}

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com;
 
public class Main {
 
  public static void main(String args[]) {
 
    // 查找文本
    {
      String src = "This is my small example string which I'm going to use for pattern matching.";
      RegexString string = RegexString.with(src).pattern("\\w+").start();
      while (string.find()) {
        System.out.println(string.matchStart() + "," + string.matchEnd() + " : " + string.matchString());
      }
    }
 
    // 匹配
    {
      String src = "This is my small example string which I'm going to use for pattern matching.";
      if (RegexString.with(src).pattern("^This.+$").start().matches()) {
        System.out.println("Yes");
      }
    }
 
    // 替換文本
    {
      String src = "This is my small example string which I'm going to use for pattern matching.";
      System.out.println(RegexString.with(src).pattern("\\w+").start().replace("Regex"));
    }
    // 去掉字符串首尾的空格,以及字符串中間多余的字符串
    {
      String src = "  This  is  my small example string  which I'm  going to  use for pattern matching.   ";
      String tmp = RegexString.with(src).pattern("^\\s+|\\s+$").start().replace("");
      String des = RegexString.with(tmp).pattern("\\s+").start().replace(" ");
      System.out.println("\"" + des + "\"");
    }
  }
}

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!

原文鏈接:http://www.cnblogs.com/YouXianMing/p/6782423.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美白虎逼 | 四虎国产视频 | 無码一区中文字幕少妇熟女网站 | 亚洲妇熟xxxxx妇色黄 | 免费xxxxx大片在线观看影视 | 五月天国产视频 | 亚洲成人福利网站 | 俄罗斯美女大逼 | 国产一区在线播放 | 千金在线观看 | 欧美一区二区日韩一区二区 | 无人区尖叫之夜美女姐姐视频 | 四虎影库网址 | 亚洲视频99 | 好男人资源免费播放 | 日本不卡在线一区二区三区视频 | 欧美日韩国产在线一区 | 亚洲天堂精品在线观看 | 欧美在线国产 | 国产a免费| www.俺去啦 | 免费看伦理片 | 双子母性本能在线观看 | 亚欧洲乱码专区视频 | 欧美国产在线视频 | 久久精品无码一区二区日韩av | 国产亚洲精品第一综合linode | 喘息揉弄1v1h老师 | 国产特级 | 60老妇性xxxxhd | 亚洲国产精品牛在线 | 精品国产自在现线久久 | 奇米影视99 | 亚洲精品国产国语 | 幻女free性zoz0交 | 国产精品密播放国产免费看 | 成人影院在线观看免费 | 亚洲欧美另类在线观看 | 小早川怜子在线播放精品 | 青青操在线播放 | 国色天香 社区视频 |