@@ -11,24 +11,82 @@ library author [Philip Hazel](https://github.com/PhilipHazel) and its contributo
11
11
12
12
## Usage
13
13
14
- To use the PCRE4J library in your project, add the following dependency to your ` pom.xml ` file:
14
+ The PCRE4J library provides several APIs to interact with the PCRE library:
15
+ - ` java.util.regex ` -alike API via ` org.pcre4j.regex.Pattern ` and ` org.pcre4j.regex.Matcher `
16
+ - The PCRE4J API via ` org.pcre4j.Pcre2Code ` and related classes
17
+ - The ` libpcre2 ` direct API via backends that implement ` org.pcre4j.api.IPcre2 `
18
+
19
+ ### Quick Start with ` java.util.regex ` -alike API
20
+
21
+ Add the following dependencies to your ` pom.xml ` file:
22
+
23
+ ``` xml
24
+ <dependencies >
25
+ <dependency >
26
+ <groupId >org.pcre4j</groupId >
27
+ <artifactId >regex</artifactId >
28
+ <version >0.0.0</version >
29
+ </dependency >
30
+ <dependency >
31
+ <groupId >org.pcre4j</groupId >
32
+ <!-- TODO: Select one of the following artifacts corresponding to the backend you want to use -->
33
+ <artifactId >jna</artifactId >
34
+ <!-- <artifactId>ffm</artifactId> -->
35
+ <version >0.0.0</version >
36
+ </dependency >
37
+ </dependencies >
38
+ ```
39
+
40
+ Proceed using the PCRE4J library in your Java code similarly like if you were using the ` java.util.regex ` package:
41
+
42
+ ``` java
43
+ import org.pcre4j.Pcre4j ;
44
+ // TODO: Select one of the following imports for the backend you want to use:
45
+ import org.pcre4j.jna.Pcre2 ;
46
+ // import org.pcre4j.ffm.Pcre2;
47
+ import org.pcre4j.regex.Pattern ;
48
+
49
+ public class Usage {
50
+ static {
51
+ Pcre4j . setup(new Pcre2 ());
52
+ }
53
+
54
+ public static String [] example (String pattern , String subject ) {
55
+ final var matcher = Pattern . compile(pattern). matcher(subject);
56
+ if (matcher. find()) {
57
+ final var groups = new String [matcher. groupCount() + 1 ];
58
+ for (var i = 0 ; i < groups. length; i++ ) {
59
+ groups[i] = matcher. group(i);
60
+ }
61
+ return groups;
62
+ }
63
+ return null ;
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### Advanced Usage via PCRE4J API
69
+
70
+ Add the following dependencies to your ` pom.xml ` file:
15
71
16
72
``` xml
17
- <dependency >
18
- <groupId >org.pcre4j</groupId >
19
- <artifactId >lib</artifactId >
20
- <version >0.0.0</version >
21
- </dependency >
22
- <dependency >
23
- <groupId >org.pcre4j</groupId >
24
- <!-- TODO: Select one of the following artifacts corresponding to the backend you want to use -->
25
- <artifactId >jna</artifactId >
26
- <!-- <artifactId>ffm</artifactId> -->
27
- <version >0.0.0</version >
28
- </dependency >
73
+ <dependencies >
74
+ <dependency >
75
+ <groupId >org.pcre4j</groupId >
76
+ <artifactId >lib</artifactId >
77
+ <version >0.0.0</version >
78
+ </dependency >
79
+ <dependency >
80
+ <groupId >org.pcre4j</groupId >
81
+ <!-- TODO: Select one of the following artifacts corresponding to the backend you want to use -->
82
+ <artifactId >jna</artifactId >
83
+ <!-- <artifactId>ffm</artifactId> -->
84
+ <version >0.0.0</version >
85
+ </dependency >
86
+ </dependencies >
29
87
```
30
88
31
- Then, you can use the PCRE4J library in your Java code as follows :
89
+ Proceed using the PCRE4J library in your Java code:
32
90
33
91
``` java
34
92
import org.pcre4j.Pcre2Code ;
@@ -65,6 +123,47 @@ public class Usage {
65
123
}
66
124
```
67
125
126
+ ### Low-Level Usage
127
+
128
+ Add the following dependencies to your ` pom.xml ` file:
129
+
130
+ ``` xml
131
+ <dependencies >
132
+ <dependency >
133
+ <groupId >org.pcre4j</groupId >
134
+ <!-- TODO: Select one of the following artifacts corresponding to the backend you want to use -->
135
+ <artifactId >jna</artifactId >
136
+ <!-- <artifactId>ffm</artifactId> -->
137
+ <version >0.0.0</version >
138
+ </dependency >
139
+ </dependencies >
140
+ ```
141
+
142
+ Proceed using the ` libpcre2 ` API in your Java code:
143
+
144
+ ``` java
145
+ // TODO: Select one of the following imports for the backend you want to use:
146
+ import org.pcre4j.jna.Pcre2 ;
147
+ // import org.pcre4j.ffm.Pcre2;
148
+
149
+ public class Usage {
150
+ public static void example () {
151
+ final var pcre2 = new Pcre2 ();
152
+
153
+ final var errorcode = new int [1 ];
154
+ final var erroroffset = new long [1 ];
155
+ final var code = pcre2. compile(" pattern" , 0 , errorcode, erroroffset, 0 );
156
+ if (code == 0 ) {
157
+ throw new RuntimeException (
158
+ " PCRE2 compilation failed with error code " + errorcode[0 ] + " at offset " + erroroffset[0 ]
159
+ );
160
+ }
161
+
162
+ api. codeFree(code);
163
+ }
164
+ }
165
+ ```
166
+
68
167
## Backends
69
168
70
169
The PCRE4J library supports several backends to invoke the ` pcre2 ` API.
0 commit comments