Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit c60ceb5

Browse files
✨ PatternMaker!
1 parent 4da7290 commit c60ceb5

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
4.39 KB
Binary file not shown.

‎src/SomeUtils/Pattern/PatternMaker.java

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package SomeUtils.Pattern;
2+
3+
import java.util.regex.Pattern;
4+
5+
public class PatternMaker{
6+
//Just a demo.
7+
public static void main(String[]a){
8+
final PatternMaker maker=new PatternMaker();
9+
final String sample="blah blach blah ...";
10+
//Translates into:
11+
//Match string "bla" and any character that occurs atleast once before a whitespace.
12+
//After that, remember the match and see if it matches the string atleast once.
13+
//Lastly, check if there are any more characters that occurs atleast once.
14+
final Pattern pattern=maker.add("bla")
15+
.any()
16+
.atleastOnce()
17+
.whitespace()
18+
.captureRecent()
19+
.atleast(1)
20+
.any()
21+
.atleastOnce()
22+
.compile();
23+
System.out.println("This is what the pattern looks like:");
24+
System.out.println(pattern.toString());
25+
System.out.println("Does it matches our sample string: "+sample+" ?");
26+
System.out.println((pattern.matcher(sample).matches())?"Yes it does!":"Nope, it doesn't.");
27+
System.out.println("This is what it looks like if we replace any match with just the letter 'e':");
28+
System.out.println(pattern.matcher(sample).replaceAll("e"));
29+
}
30+
//The class itself.
31+
final StringBuilder builder=new StringBuilder();
32+
public PatternMaker(){}
33+
public PatternMaker(final String in){
34+
builder.append(in);
35+
}
36+
public PatternMaker(final Pattern in){
37+
builder.append(in.toString());
38+
}
39+
public PatternMaker add(final String in){
40+
builder.append(in);
41+
return this;
42+
}
43+
public PatternMaker anyOf(final String in){
44+
builder.append("[").append(in).append("]");
45+
return this;
46+
}
47+
public PatternMaker notAnyOf(final String in){
48+
builder.append("[^").append(in).append("]");
49+
return this;
50+
}
51+
public PatternMaker rangeOf(final char in, final char in2){
52+
builder.append(Character.toString(in))
53+
.append("-")
54+
.append(Character.toString(in2));
55+
return this;
56+
}
57+
public PatternMaker or(final String in){
58+
builder.append("|").append(in);
59+
return this;
60+
}
61+
public PatternMaker any(){
62+
builder.append(".");
63+
return this;
64+
}
65+
public PatternMaker beginsWith(final String in){
66+
builder.append("^").append(in);
67+
return this;
68+
}
69+
public PatternMaker endsWith(final String in){
70+
builder.append(in).append("$");
71+
return this;
72+
}
73+
public PatternMaker anyNumber(){
74+
builder.append("\\d");
75+
return this;
76+
}
77+
public PatternMaker whitespace(){
78+
builder.append("\\s");
79+
return this;
80+
}
81+
public PatternMaker wordBoundary(){
82+
builder.append("\\b");
83+
return this;
84+
}
85+
public PatternMaker unicode(final String in){
86+
if(in.startsWith("\\u"))
87+
builder.append(Character.toString(
88+
(char)Integer.parseInt(in.substring(2), 16)
89+
));
90+
else
91+
builder.append(Character.toString(
92+
(char)Integer.parseInt(in, 16)
93+
));
94+
return this;
95+
}
96+
public PatternMaker once(){
97+
builder.append("{1}");
98+
return this;
99+
}
100+
public PatternMaker noneOrMore(){
101+
builder.append("*");
102+
return this;
103+
}
104+
public PatternMaker onceOrNone(){
105+
builder.append("?");
106+
return this;
107+
}
108+
public PatternMaker atleastOnce(){
109+
builder.append("+");
110+
return this;
111+
}
112+
public PatternMaker occurOnly(final int in){
113+
builder.append("{").append(String.valueOf(in)).append("}");
114+
return this;
115+
}
116+
public PatternMaker atleast(final int in){
117+
builder.append("{").append(String.valueOf(in)).append(",}");
118+
return this;
119+
}
120+
public PatternMaker atleastRange(final int from, final int to){
121+
builder.append("{")
122+
.append(String.valueOf(from))
123+
.append(",")
124+
.append(String.valueOf(to))
125+
.append("}");
126+
return this;
127+
}
128+
public PatternMaker captureRecent(){
129+
builder.insert(0, "(").append(")");
130+
return this;
131+
}
132+
133+
/**From Pattern.java
134+
*Go to their website for more details:
135+
*https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/tip...
136+
*.../src/share/classes/java/util/regex/Pattern.java
137+
**/
138+
public static final int UNIX_LINES=0x01;
139+
public static final int CASE_INSENSITIVE=0x02;
140+
public static final int COMMENTS=0x04;
141+
public static final int MULTILINE=0x08;
142+
public static final int LITERAL=0x10;
143+
public static final int DOTALL=0x20;
144+
public static final int CANON_EQ=0x80;
145+
public static final int UNICODE_CHARACTER_CLASS=0x100;
146+
147+
public Pattern compile(){
148+
return Pattern.compile(builder.toString());
149+
}
150+
public Pattern compile(final int in){
151+
return Pattern.compile(builder.toString(), in);
152+
}
153+
@Override
154+
public String toString(){
155+
return builder.toString();
156+
}
157+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /