|
1 | 1 | joni |
2 | 2 | ==== |
3 | 3 |
|
4 | | -[](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.jruby.joni%22) |
5 | | -[](https://github.com/jruby/joni/actions) |
| 4 | +[](https://central.sonatype.com/artifact/org.jruby.joni/joni) |
| 5 | +[](https://github.com/jruby/joni/actions/workflows/maven.yml) |
6 | 6 |
|
7 | | -Java port of Oniguruma regexp library |
| 7 | +Java port of [Oniguruma](https://github.com/kkos/oniguruma) regexp library |
8 | 8 |
|
9 | 9 | ## Usage |
10 | 10 |
|
11 | 11 | ### Imports |
12 | | - ```java |
13 | | - import org.jcodings.specific.UTF8Encoding; |
14 | | - import org.joni.Matcher; |
15 | | - import org.joni.Option; |
16 | | - import org.joni.Regex; |
17 | | - ``` |
| 12 | + |
| 13 | +```java |
| 14 | +import org.jcodings.specific.UTF8Encoding; |
| 15 | +import org.joni.Matcher; |
| 16 | +import org.joni.Option; |
| 17 | +import org.joni.Regex; |
| 18 | +``` |
18 | 19 |
|
19 | 20 | ### Matching |
20 | 21 |
|
21 | | - ```java |
22 | | - |
23 | | - byte[] pattern = "a*".getBytes(); |
24 | | - byte[] str = "aaa".getBytes(); |
| 22 | +```java |
| 23 | +byte[] pattern = "a*".getBytes(); |
| 24 | +byte[] str = "aaa".getBytes(); |
25 | 25 |
|
26 | | - Regex regex = new Regex(pattern, 0, pattern.length, Option.NONE, UTF8Encoding.INSTANCE); |
27 | | - Matcher matcher = regex.matcher(str); |
28 | | - int result = matcher.search(0, str.length, Option.DEFAULT); |
29 | | - ``` |
| 26 | +Regex regex = new Regex(pattern, 0, pattern.length, Option.NONE, UTF8Encoding.INSTANCE); |
| 27 | +Matcher matcher = regex.matcher(str); |
| 28 | +int result = matcher.search(0, str.length, Option.DEFAULT); |
| 29 | +``` |
30 | 30 |
|
31 | 31 | ### Using captures |
32 | 32 |
|
33 | | - ```java |
34 | | - byte[] pattern = "(a*)".getBytes(); |
35 | | - byte[] str = "aaa".getBytes(); |
| 33 | +```java |
| 34 | +byte[] pattern = "(a*)".getBytes(); |
| 35 | +byte[] str = "aaa".getBytes(); |
36 | 36 |
|
37 | | - Regex regex = new Regex(pattern, 0, pattern.length, Option.NONE, UTF8Encoding.INSTANCE); |
38 | | - Matcher matcher = regex.matcher(str); |
39 | | - int result = matcher.search(0, str.length, Option.DEFAULT); |
40 | | - if (result != -1) { |
41 | | - Region region = matcher.getEagerRegion(); |
42 | | - } |
43 | | - ``` |
| 37 | +Regex regex = new Regex(pattern, 0, pattern.length, Option.NONE, UTF8Encoding.INSTANCE); |
| 38 | +Matcher matcher = regex.matcher(str); |
| 39 | +int result = matcher.search(0, str.length, Option.DEFAULT); |
| 40 | +if (result != -1) { |
| 41 | + Region region = matcher.getEagerRegion(); |
| 42 | +} |
| 43 | +``` |
44 | 44 |
|
45 | 45 | ### Using named captures |
46 | 46 |
|
47 | | - ```java |
48 | | - byte[] pattern = "(?<name>a*)".getBytes(); |
49 | | - byte[] str = "aaa".getBytes(); |
50 | | - |
51 | | - Regex regex = new Regex(pattern, 0, pattern.length, Option.NONE, UTF8Encoding.INSTANCE); |
52 | | - Matcher matcher = regex.matcher(str); |
53 | | - int result = matcher.search(0, str.length, Option.DEFAULT); |
54 | | - if (result != -1) { |
55 | | - Region region = matcher.getEagerRegion(); |
56 | | - for (Iterator<NameEntry> entry = regex.namedBackrefIterator(); entry.hasNext();) { |
57 | | - NameEntry e = entry.next(); |
58 | | - int number = e.getBackRefs()[0]; // can have many refs per name |
59 | | - // int begin = region.beg[number]; |
60 | | - // int end = region.end[number]; |
61 | | - |
62 | | - } |
| 47 | +```java |
| 48 | +byte[] pattern = "(?<name>a*)".getBytes(); |
| 49 | +byte[] str = "aaa".getBytes(); |
| 50 | + |
| 51 | +Regex regex = new Regex(pattern, 0, pattern.length, Option.NONE, UTF8Encoding.INSTANCE); |
| 52 | +Matcher matcher = regex.matcher(str); |
| 53 | +int result = matcher.search(0, str.length, Option.DEFAULT); |
| 54 | +if (result != -1) { |
| 55 | + Region region = matcher.getEagerRegion(); |
| 56 | + for (Iterator<NameEntry> entry = regex.namedBackrefIterator(); entry.hasNext();) { |
| 57 | + NameEntry e = entry.next(); |
| 58 | + int number = e.getBackRefs()[0]; // can have many refs per name |
| 59 | + // int begin = region.beg[number]; |
| 60 | + // int end = region.end[number]; |
63 | 61 | } |
64 | | - ``` |
| 62 | +} |
| 63 | +``` |
65 | 64 |
|
66 | 65 | ## License |
67 | 66 |
|
|
0 commit comments