cloudroam
2025-03-10 c306cba52bcc3e2c423f77d4a52c35ad04c52038
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
package com.jsh.erp.utils;
 
import org.springframework.util.Assert;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by Adm on 2015/12/14.
 *
 * @author yubiao
 *         <p/>
 *         mysql匹配正则表达式
 */
public class RegExpTools {
    /**
     * @param search 模糊匹配字符串数组
     */
    public static String regexp(List<String> search) {
        if (search == null || search.isEmpty())
            return null;
        String regexp = "";
        for (String s : search) {
            if (!regexp.isEmpty()) {
                regexp = regexp + "|";
            }
            regexp = regexp + ".*";
            regexp = regexp + s.replaceAll("\\.", "\\\\.");
            regexp = regexp + ".*";
        }
        return regexp;
    }
 
    /**
     * @param key    json字段key
     * @param search 模糊匹配字符串数组
     *               json的mysql匹配正则表达式
     */
    public static String regexp(String key, List<String> search) {
        if (search == null || search.isEmpty())
            return null;
        StringBuilder sb = new StringBuilder();
        for (String s : search) {
            if (sb.length() == 0) {
                sb.append(".*\\\"").append(key).append("\\\":\\\"[a-zA-Z0-9]*(");
            } else {
                sb.append("|");
            }
            sb.append(s);
        }
        sb.append(")[a-zA-Z0-9]*\\\".*");
        return sb.toString();
    }
 
    public static class RegExp {
        public static final String ANY = ".*";
        public static final String QUOTE = "\\\"";
        public static final String LFT_PAREN = "(";
        public static final String RHT_PAREN = ")";
        public static final String COLON = ":";
        public static final String OR = "|";
 
        private final StringBuilder builder = new StringBuilder();
 
        public RegExp any() {
            builder.append(ANY);
            return this;
        }
 
        public RegExp lftParen() {
            builder.append(LFT_PAREN);
            return this;
        }
 
        public RegExp rhtParen() {
            builder.append(RHT_PAREN);
            return this;
        }
 
        public RegExp colon() {
            builder.append(COLON);
            return this;
 
        }
 
        public RegExp quote() {
            builder.append(QUOTE);
            return this;
        }
 
        public RegExp quote(String str) {
            Assert.notNull(str, "str为空");
            builder.append(QUOTE).append(str).append(QUOTE);
            return this;
        }
 
        public RegExp value(String str) {
            Assert.notNull(str, "str为空");
            builder.append(str);
            return this;
        }
 
        public RegExp or() {
            builder.append(OR);
            return this;
        }
 
        public RegExp or(List<String> values) {
            Assert.notEmpty(values, "values必须非空");
            lftParen();
            boolean first = true;
            for (String value : values) {
                if (first) {
                    builder.append(value);
                    first = false;
                } else {
                    builder.append(OR).append(value);
                }
            }
            rhtParen();
            return this;
        }
 
        @Override
        public String toString() {
            return builder.toString();
        }
 
        public static void main(String[] args) {
            List<String> values = new ArrayList<String>();
 
            values.add("310");
            values.add(String.valueOf(2));
            values.add(String.valueOf(3));
 
            RegExp exp = new RegExp();
 
            exp.any();
            exp.quote("fullKbNum").colon()
                    .quote()
                    .value("[a-zA-Z0-9]*").or(values).value("[a-zA-Z0-9]*")
                    .quote();
            exp.or();
            exp.quote("gbId[a-f0-9-]{36}").colon()
                    .quote()
                    .value("[0-9]*").or(values).value("[0-9]*")
                    .quote();
            exp.any();
 
            System.out.println(exp);
        }
 
    }
}