Panduan Untuk API Ekspresi Biasa Java

1. Gambaran keseluruhan

Dalam artikel ini, kita akan membahas Java Regex API dan bagaimana ungkapan biasa dapat digunakan dalam bahasa pemrograman Java.

Dalam dunia ungkapan biasa, terdapat banyak rasa yang boleh dipilih, seperti grep, Perl, Python, PHP, awk dan banyak lagi.

Ini bermaksud bahawa ungkapan biasa yang berfungsi dalam satu bahasa pengaturcaraan mungkin tidak berfungsi dalam bahasa yang lain. Sintaks ungkapan biasa di Java paling serupa dengan yang terdapat di Perl.

2. Persediaan

Untuk menggunakan ungkapan biasa di Java, kami tidak memerlukan penyediaan khas. JDK mengandungi pakej khas java.util.regex yang dikhaskan sepenuhnya untuk operasi regex. Kami hanya perlu mengimportnya ke dalam kod kami.

Lebih-lebih lagi, kelas java.lang.String juga mempunyai sokongan regex bawaan yang biasa kita gunakan dalam kod kita.

3. Pakej Java Regex

The java.util.regex pakej terdiri daripada tiga kelas: corak, Matcher dan PatternSyntaxException:

  • Objek corak adalah regex yang disusun. The Corak kelas tidak memberikan pengeluar awam. Untuk membuat corak, pertama kita mesti menggunakan salah satu kaedah penyusun statik awamnya , yang kemudian akan mengembalikan objek Corak . Kaedah ini menerima ungkapan biasa sebagai hujah pertama.
  • Objek matcher menafsirkan corak dan melakukan operasi padanan terhadap String input . Ia juga tidak menentukan pembina awam. Kami memperoleh objek Matcher dengan menggunakan kaedah matcher pada objek Pattern .
  • Objek PatternSyntaxException adalah pengecualian yang tidak dicentang yang menunjukkan ralat sintaks dalam corak ekspresi biasa.

Kami akan meneroka kelas-kelas ini secara terperinci; namun, kita mesti terlebih dahulu memahami bagaimana regex dibina di Jawa.

Sekiranya anda sudah biasa dengan regex dari persekitaran yang berbeza, anda mungkin menemui perbezaan tertentu, tetapi perbezaannya minimum.

4. Contoh Ringkas

Mari mulakan dengan kes penggunaan paling mudah untuk regex. Seperti yang kita perhatikan sebelumnya, ketika regex diterapkan pada String, itu mungkin sepadan dengan sifar atau lebih banyak kali.

Bentuk padanan corak yang paling asas yang disokong oleh API java.util.regex adalah padanan literal String . Contohnya, jika ungkapan biasa adalah foo dan input String adalah foo , padanan akan berjaya kerana Strings serupa:

@Test public void givenText_whenSimpleRegexMatches_thenCorrect() { Pattern pattern = Pattern.compile("foo"); Matcher matcher = pattern.matcher("foo"); assertTrue(matcher.find()); }

Kami mula-mula membuat objek Corak dengan memanggil kaedah penyusun statiknya dan meneruskannya sebagai corak yang ingin kita gunakan.

Kemudian kami membuat objek Matcher memanggil kaedah pencocokan objek Pola dan menyampaikannya teks yang ingin kami periksa untuk pertandingan.

Selepas itu, kami memanggil kaedah cari di objek Matcher.

The find kaedah menyimpan memajukan melalui teks input dan pulangan benar bagi setiap perlawanan, jadi kami boleh menggunakannya untuk mencari kiraan perlawanan juga:

@Test public void givenText_whenSimpleRegexMatchesTwice_thenCorrect() { Pattern pattern = Pattern.compile("foo"); Matcher matcher = pattern.matcher("foofoo"); int matches = 0; while (matcher.find()) { matches++; } assertEquals(matches, 2); }

Oleh kerana kita akan menjalankan lebih banyak ujian, kita dapat menyusun logik untuk mencari bilangan perlawanan dalam kaedah yang disebut runTest :

public static int runTest(String regex, String text) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); int matches = 0; while (matcher.find()) { matches++; } return matches; }

Apabila kita mendapat 0 perlawanan, ujian akan gagal, jika tidak, ia akan lulus.

5. Watak Meta

Meta watak mempengaruhi cara corak dicocokkan, dengan cara menambahkan logik pada corak carian. Java API menyokong beberapa metakarakter, yang paling mudah adalah titik "." yang sesuai dengan mana-mana watak:

@Test public void givenText_whenMatchesWithDotMetach_thenCorrect() { int matches = runTest(".", "foo"); assertTrue(matches > 0); }

Mengingat contoh sebelumnya di mana regex foo memadankan teks foo dan foofoo dua kali. Sekiranya kita menggunakan titik metakarakter dalam regex, kita tidak akan mendapat dua perlawanan dalam kes kedua:

@Test public void givenRepeatedText_whenMatchesOnceWithDotMetach_thenCorrect() { int matches= runTest("foo.", "foofoo"); assertEquals(matches, 1); }

Perhatikan titik selepas foo di regex. Pencocokan memadankan setiap teks yang didahului oleh foo kerana bahagian titik terakhir bermaksud watak selepasnya. Jadi setelah menemui foo pertama , selebihnya dilihat sebagai watak apa pun. Itulah sebabnya hanya ada satu perlawanan.

API menyokong beberapa watak meta lain yang akan kita kaji lebih jauh dalam artikel ini.

6. Kelas Perwatakan

Melayari spesifikasi kelas Corak rasmi , kami akan menemui ringkasan konstruk regex yang disokong. Di bawah kelas watak, kami mempunyai kira-kira 6 konstruk.

6.1. ATAU Kelas

Dibina sebagai [abc] . Mana-mana elemen dalam set sesuai:

@Test public void givenORSet_whenMatchesAny_thenCorrect() { int matches = runTest("[abc]", "b"); assertEquals(matches, 1); }

Sekiranya semuanya muncul dalam teks, masing-masing dipadankan secara terpisah tanpa memperhatikan urutan:

@Test public void givenORSet_whenMatchesAnyAndAll_thenCorrect() { int matches = runTest("[abc]", "cab"); assertEquals(matches, 3); }

Mereka juga boleh diganti sebagai sebahagian daripada String . Dalam contoh berikut, apabila kita membuat kata-kata yang berbeza dengan mengganti huruf pertama dengan setiap elemen set, semuanya dipadankan:

@Test public void givenORSet_whenMatchesAllCombinations_thenCorrect() { int matches = runTest("[bcr]at", "bat cat rat"); assertEquals(matches, 3); }

6.2. Kelas NOR

Set di atas ditolak dengan menambahkan karet sebagai elemen pertama:

@Test public void givenNORSet_whenMatchesNon_thenCorrect() { int matches = runTest("[^abc]", "g"); assertTrue(matches > 0); }

Kes lain:

@Test public void givenNORSet_whenMatchesAllExceptElements_thenCorrect() { int matches = runTest("[^bcr]at", "sat mat eat"); assertTrue(matches > 0); }

6.3. Kelas Julat

Kita dapat menentukan kelas yang menentukan julat di mana teks yang sesuai harus jatuh menggunakan tanda hubung (-), begitu juga, kita juga dapat meniadakan julat.

Huruf besar yang sepadan:

@Test public void givenUpperCaseRange_whenMatchesUpperCase_ thenCorrect() { int matches = runTest( "[A-Z]", "Two Uppercase alphabets 34 overall"); assertEquals(matches, 2); }

Huruf kecil yang sepadan:

@Test public void givenLowerCaseRange_whenMatchesLowerCase_ thenCorrect() { int matches = runTest( "[a-z]", "Two Uppercase alphabets 34 overall"); assertEquals(matches, 26); }

Memadankan huruf besar dan huruf kecil:

@Test public void givenBothLowerAndUpperCaseRange_ whenMatchesAllLetters_thenCorrect() { int matches = runTest( "[a-zA-Z]", "Two Uppercase alphabets 34 overall"); assertEquals(matches, 28); }

Memadankan julat nombor yang ditentukan:

@Test public void givenNumberRange_whenMatchesAccurately_ thenCorrect() { int matches = runTest( "[1-5]", "Two Uppercase alphabets 34 overall"); assertEquals(matches, 2); }

Memadankan julat nombor yang lain:

@Test public void givenNumberRange_whenMatchesAccurately_ thenCorrect2(){ int matches = runTest( "[30-35]", "Two Uppercase alphabets 34 overall"); assertEquals(matches, 1); }

6.4. Kelas Kesatuan

Kelas watak kesatuan adalah hasil gabungan dua atau lebih kelas watak:

@Test public void givenTwoSets_whenMatchesUnion_thenCorrect() { int matches = runTest("[1-3[7-9]]", "123456789"); assertEquals(matches, 6); }

Ujian di atas hanya akan sepadan dengan 6 daripada 9 bilangan bulat kerana kesatuan yang melangkau 4, 5 dan 6.

6.5. Kelas Persimpangan

Sama seperti kelas kesatuan, kelas ini berpunca daripada memilih elemen sepunya antara dua atau lebih set. Untuk menggunakan persimpangan, kami menggunakan && :

@Test public void givenTwoSets_whenMatchesIntersection_thenCorrect() { int matches = runTest("[1-6&&[3-9]]", "123456789"); assertEquals(matches, 4); }

Kami mendapat 4 perlawanan kerana persimpangan dua set hanya mempunyai 4 elemen.

6.6. Kelas Penolakan

Kita boleh menggunakan pengurangan untuk menolak satu atau lebih kelas aksara, misalnya memadankan sekumpulan nombor perpuluhan ganjil:

@Test public void givenSetWithSubtraction_whenMatchesAccurately_thenCorrect() { int matches = runTest("[0-9&&[^2468]]", "123456789"); assertEquals(matches, 5); }

Hanya 1,3,5,7,9 yang akan dipadankan.

7. Kelas Perwatakan yang Ditentukan

Java regex API juga menerima kelas watak yang telah ditentukan. Beberapa kelas watak di atas dapat dinyatakan dalam bentuk yang lebih pendek walaupun membuat kod kurang intuitif. Salah satu aspek khas dari versi Java dari regex ini adalah watak melarikan diri.

Seperti yang akan kita lihat, kebanyakan watak akan dimulai dengan garis miring terbalik, yang memiliki arti khusus di Jawa. Untuk ini disusun oleh kelas Corak - garis miring belakang mesti dilarikan iaitu \ d menjadi \\ d .

Matching digits, equivalent to [0-9]:

@Test public void givenDigits_whenMatches_thenCorrect() { int matches = runTest("\\d", "123"); assertEquals(matches, 3); }

Matching non-digits, equivalent to [^0-9]:

@Test public void givenNonDigits_whenMatches_thenCorrect() { int mathces = runTest("\\D", "a6c"); assertEquals(matches, 2); }

Matching white space:

@Test public void givenWhiteSpace_whenMatches_thenCorrect() { int matches = runTest("\\s", "a c"); assertEquals(matches, 1); }

Matching non-white space:

@Test public void givenNonWhiteSpace_whenMatches_thenCorrect() { int matches = runTest("\\S", "a c"); assertEquals(matches, 2); }

Matching a word character, equivalent to [a-zA-Z_0-9]:

@Test public void givenWordCharacter_whenMatches_thenCorrect() { int matches = runTest("\\w", "hi!"); assertEquals(matches, 2); }

Matching a non-word character:

@Test public void givenNonWordCharacter_whenMatches_thenCorrect() { int matches = runTest("\\W", "hi!"); assertEquals(matches, 1); }

8. Quantifiers

The Java regex API also allows us to use quantifiers. These enable us to further tweak the match's behavior by specifying the number of occurrences to match against.

To match a text zero or one time, we use the ? quantifier:

@Test public void givenZeroOrOneQuantifier_whenMatches_thenCorrect() { int matches = runTest("\\a?", "hi"); assertEquals(matches, 3); }

Alternatively, we can use the brace syntax, also supported by the Java regex API:

@Test public void givenZeroOrOneQuantifier_whenMatches_thenCorrect2() { int matches = runTest("\\a{0,1}", "hi"); assertEquals(matches, 3); }

This example introduces the concept of zero-length matches. It so happens that if a quantifier's threshold for matching is zero, it always matches everything in the text including an empty String at the end of every input. This means that even if the input is empty, it will return one zero-length match.

This explains why we get 3 matches in the above example despite having a String of length two. The third match is zero-length empty String.

To match a text zero or limitless times, we us * quantifier, it is just similar to ?:

@Test public void givenZeroOrManyQuantifier_whenMatches_thenCorrect() { int matches = runTest("\\a*", "hi"); assertEquals(matches, 3); }

Supported alternative:

@Test public void givenZeroOrManyQuantifier_whenMatches_thenCorrect2() { int matches = runTest("\\a{0,}", "hi"); assertEquals(matches, 3); }

The quantifier with a difference is +, it has a matching threshold of 1. If the required String does not occur at all, there will be no match, not even a zero-length String:

@Test public void givenOneOrManyQuantifier_whenMatches_thenCorrect() { int matches = runTest("\\a+", "hi"); assertFalse(matches); }

Supported alternative:

@Test public void givenOneOrManyQuantifier_whenMatches_thenCorrect2() { int matches = runTest("\\a{1,}", "hi"); assertFalse(matches); }

As it is in Perl and other languages, the brace syntax can be used to match a given text a number of times:

@Test public void givenBraceQuantifier_whenMatches_thenCorrect() { int matches = runTest("a{3}", "aaaaaa"); assertEquals(matches, 2); }

In the above example, we get two matches since a match occurs only if a appears three times in a row. However, in the next test we won't get a match since the text only appears two times in a row:

@Test public void givenBraceQuantifier_whenFailsToMatch_thenCorrect() { int matches = runTest("a{3}", "aa"); assertFalse(matches > 0); }

When we use a range in the brace, the match will be greedy, matching from the higher end of the range:

@Test public void givenBraceQuantifierWithRange_whenMatches_thenCorrect() { int matches = runTest("a{2,3}", "aaaa"); assertEquals(matches, 1); }

We've specified at least two occurrences but not exceeding three, so we get a single match instead where the matcher sees a single aaa and a lone a which can't be matched.

However, the API allows us to specify a lazy or reluctant approach such that the matcher can start from the lower end of the range in which case matching two occurrences as aa and aa:

@Test public void givenBraceQuantifierWithRange_whenMatchesLazily_thenCorrect() { int matches = runTest("a{2,3}?", "aaaa"); assertEquals(matches, 2); }

9. Capturing Groups

The API also allows us to treat multiple characters as a single unit through capturing groups.

It will attache numbers to the capturing groups and allow back referencing using these numbers.

In this section, we will see a few examples on how to use capturing groups in Java regex API.

Let's use a capturing group that matches only when an input text contains two digits next to each other:

@Test public void givenCapturingGroup_whenMatches_thenCorrect() { int maches = runTest("(\\d\\d)", "12"); assertEquals(matches, 1); }

The number attached to the above match is 1, using a back reference to tell the matcher that we want to match another occurrence of the matched portion of the text. This way, instead of:

@Test public void givenCapturingGroup_whenMatches_thenCorrect2() { int matches = runTest("(\\d\\d)", "1212"); assertEquals(matches, 2); }

Where there are two separate matches for the input, we can have one match but propagating the same regex match to span the entire length of the input using back referencing:

@Test public void givenCapturingGroup_whenMatchesWithBackReference_ thenCorrect() { int matches = runTest("(\\d\\d)\\1", "1212"); assertEquals(matches, 1); }

Where we would have to repeat the regex without back referencing to achieve the same result:

@Test public void givenCapturingGroup_whenMatches_thenCorrect3() { int matches = runTest("(\\d\\d)(\\d\\d)", "1212"); assertEquals(matches, 1); }

Similarly, for any other number of repetitions, back referencing can make the matcher see the input as a single match:

@Test public void givenCapturingGroup_whenMatchesWithBackReference_ thenCorrect2() { int matches = runTest("(\\d\\d)\\1\\1\\1", "12121212"); assertEquals(matches, 1); }

But if you change even the last digit, the match will fail:

@Test public void givenCapturingGroupAndWrongInput_ whenMatchFailsWithBackReference_thenCorrect() { int matches = runTest("(\\d\\d)\\1", "1213"); assertFalse(matches > 0); }

It is important not to forget the escape backslashes, this is crucial in Java syntax.

10. Boundary Matchers

The Java regex API also supports boundary matching. If we care about where exactly in the input text the match should occur, then this is what we are looking for. With the previous examples, all we cared about was whether a match was found or not.

To match only when the required regex is true at the beginning of the text, we use the caret ^.

This test will fail since the text dog can be found at the beginning:

@Test public void givenText_whenMatchesAtBeginning_thenCorrect() { int matches = runTest("^dog", "dogs are friendly"); assertTrue(matches > 0); }

The following test will fail:

@Test public void givenTextAndWrongInput_whenMatchFailsAtBeginning_ thenCorrect() { int matches = runTest("^dog", "are dogs are friendly?"); assertFalse(matches > 0); }

To match only when the required regex is true at the end of the text, we use the dollar character $. A match will be found in the following case:

@Test public void givenText_whenMatchesAtEnd_thenCorrect() { int matches = runTest("dog$", "Man's best friend is a dog"); assertTrue(matches > 0); }

And no match will be found here:

@Test public void givenTextAndWrongInput_whenMatchFailsAtEnd_thenCorrect() { int matches = runTest("dog$", "is a dog man's best friend?"); assertFalse(matches > 0); }

If we want a match only when the required text is found at a word boundary, we use \\b regex at the beginning and end of the regex:

Space is a word boundary:

@Test public void givenText_whenMatchesAtWordBoundary_thenCorrect() { int matches = runTest("\\bdog\\b", "a dog is friendly"); assertTrue(matches > 0); }

The empty string at the beginning of a line is also a word boundary:

@Test public void givenText_whenMatchesAtWordBoundary_thenCorrect2() { int matches = runTest("\\bdog\\b", "dog is man's best friend"); assertTrue(matches > 0); }

These tests pass because the beginning of a String, as well as space between one text and another, marks a word boundary, however, the following test shows the opposite:

@Test public void givenWrongText_whenMatchFailsAtWordBoundary_thenCorrect() { int matches = runTest("\\bdog\\b", "snoop dogg is a rapper"); assertFalse(matches > 0); }

Two-word characters appearing in a row does not mark a word boundary, but we can make it pass by changing the end of the regex to look for a non-word boundary:

@Test public void givenText_whenMatchesAtWordAndNonBoundary_thenCorrect() { int matches = runTest("\\bdog\\B", "snoop dogg is a rapper"); assertTrue(matches > 0); }

11. Pattern Class Methods

Previously, we have only created Pattern objects in a basic way. However, this class has another variant of the compile method that accepts a set of flags alongside the regex argument affecting the way the pattern is matched.

These flags are simply abstracted integer values. Let's overload the runTest method in the test class so that it can take a flag as the third argument:

public static int runTest(String regex, String text, int flags) { pattern = Pattern.compile(regex, flags); matcher = pattern.matcher(text); int matches = 0; while (matcher.find()){ matches++; } return matches; }

In this section, we will look at the different supported flags and how they are used.

Pattern.CANON_EQ

This flag enables canonical equivalence. When specified, two characters will be considered to match if, and only if, their full canonical decompositions match.

Consider the accented Unicode character é. Its composite code point is u00E9. However, Unicode also has a separate code point for its component characters e, u0065 and the acute accent, u0301. In this case, composite character u00E9 is indistinguishable from the two character sequence u0065 u0301.

By default, matching does not take canonical equivalence into account:

@Test public void givenRegexWithoutCanonEq_whenMatchFailsOnEquivalentUnicode_thenCorrect() { int matches = runTest("\u00E9", "\u0065\u0301"); assertFalse(matches > 0); }

But if we add the flag, then the test will pass:

@Test public void givenRegexWithCanonEq_whenMatchesOnEquivalentUnicode_thenCorrect() { int matches = runTest("\u00E9", "\u0065\u0301", Pattern.CANON_EQ); assertTrue(matches > 0); }

Pattern.CASE_INSENSITIVE

This flag enables matching regardless of case. By default matching takes case into account:

@Test public void givenRegexWithDefaultMatcher_whenMatchFailsOnDifferentCases_thenCorrect() { int matches = runTest("dog", "This is a Dog"); assertFalse(matches > 0); }

So using this flag, we can change the default behavior:

@Test public void givenRegexWithCaseInsensitiveMatcher _whenMatchesOnDifferentCases_thenCorrect() { int matches = runTest( "dog", "This is a Dog", Pattern.CASE_INSENSITIVE); assertTrue(matches > 0); }

We can also use the equivalent, embedded flag expression to achieve the same result:

@Test public void givenRegexWithEmbeddedCaseInsensitiveMatcher _whenMatchesOnDifferentCases_thenCorrect() { int matches = runTest("(?i)dog", "This is a Dog"); assertTrue(matches > 0); }

Pattern.COMMENTS

The Java API allows one to include comments using # in the regex. This can help in documenting complex regex that may not be immediately obvious to another programmer.

The comments flag makes the matcher ignore any white space or comments in the regex and only consider the pattern. In the default matching mode the following test would fail:

@Test public void givenRegexWithComments_whenMatchFailsWithoutFlag_thenCorrect() { int matches = runTest( "dog$ #check for word dog at end of text", "This is a dog"); assertFalse(matches > 0); }

This is because the matcher will look for the entire regex in the input text, including the spaces and the # character. But when we use the flag, it will ignore the extra spaces and the every text starting with # will be seen as a comment to be ignored for each line:

@Test public void givenRegexWithComments_whenMatchesWithFlag_thenCorrect() { int matches = runTest( "dog$ #check end of text","This is a dog", Pattern.COMMENTS); assertTrue(matches > 0); }

There is also an alternative embedded flag expression for this:

@Test public void givenRegexWithComments_whenMatchesWithEmbeddedFlag_thenCorrect() { int matches = runTest( "(?x)dog$ #check end of text", "This is a dog"); assertTrue(matches > 0); }

Pattern.DOTALL

By default, when we use the dot “.” expression in regex, we are matching every character in the input String until we encounter a new line character.

Using this flag, the match will include the line terminator as well. We will understand better with the following examples. These examples will be a little different. Since we are interested in asserting against the matched String, we will use matcher‘s group method which returns the previous match.

First, we will see the default behavior:

@Test public void givenRegexWithLineTerminator_whenMatchFails_thenCorrect() { Pattern pattern = Pattern.compile("(.*)"); Matcher matcher = pattern.matcher( "this is a text" + System.getProperty("line.separator") + " continued on another line"); matcher.find(); assertEquals("this is a text", matcher.group(1)); }

As we can see, only the first part of the input before the line terminator is matched.

Now in dotall mode, the entire text including the line terminator will be matched:

@Test public void givenRegexWithLineTerminator_whenMatchesWithDotall_thenCorrect() { Pattern pattern = Pattern.compile("(.*)", Pattern.DOTALL); Matcher matcher = pattern.matcher( "this is a text" + System.getProperty("line.separator") + " continued on another line"); matcher.find(); assertEquals( "this is a text" + System.getProperty("line.separator") + " continued on another line", matcher.group(1)); }

We can also use an embedded flag expression to enable dotall mode:

@Test public void givenRegexWithLineTerminator_whenMatchesWithEmbeddedDotall _thenCorrect() { Pattern pattern = Pattern.compile("(?s)(.*)"); Matcher matcher = pattern.matcher( "this is a text" + System.getProperty("line.separator") + " continued on another line"); matcher.find(); assertEquals( "this is a text" + System.getProperty("line.separator") + " continued on another line", matcher.group(1)); }

Pattern.LITERAL

When in this mode, matcher gives no special meaning to any metacharacters, escape characters or regex syntax. Without this flag, the matcher will match the following regex against any input String:

@Test public void givenRegex_whenMatchesWithoutLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text"); assertTrue(matches > 0); }

This is the default behavior we have been seeing in all the examples. However, with this flag, no match will be found, since the matcher will be looking for (.*) instead of interpreting it:

@Test public void givenRegex_whenMatchFailsWithLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text", Pattern.LITERAL); assertFalse(matches > 0); }

Now if we add the required string, the test will pass:

@Test public void givenRegex_whenMatchesWithLiteralFlag_thenCorrect() { int matches = runTest("(.*)", "text(.*)", Pattern.LITERAL); assertTrue(matches > 0); }

There is no embedded flag character for enabling literal parsing.

Pattern.MULTILINE

By default ^ and $ metacharacters match absolutely at the beginning and at the end respectively of the entire input String. The matcher disregards any line terminators:

@Test public void givenRegex_whenMatchFailsWithoutMultilineFlag_thenCorrect() { int matches = runTest( "dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox"); assertFalse(matches > 0); }

The match fails because the matcher searches for dog at the end of the entire String but the dog is present at the end of the first line of the string.

However, with the flag, the same test will pass since the matcher now takes into account line terminators. So the String dog is found just before the line terminates, hence success:

@Test public void givenRegex_whenMatchesWithMultilineFlag_thenCorrect() { int matches = runTest( "dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox", Pattern.MULTILINE); assertTrue(matches > 0); }

Here is the embedded flag version:

@Test public void givenRegex_whenMatchesWithEmbeddedMultilineFlag_ thenCorrect() { int matches = runTest( "(?m)dog$", "This is a dog" + System.getProperty("line.separator") + "this is a fox"); assertTrue(matches > 0); }

12. Matcher Class Methods

In this section, we will look at some useful methods of the Matcher class. We will group them according to functionality for clarity.

12.1. Index Methods

Index methods provide useful index values that show precisely where the match was found in the input String . In the following test, we will confirm the start and end indices of the match for dog in the input String :

@Test public void givenMatch_whenGetsIndices_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("This dog is mine"); matcher.find(); assertEquals(5, matcher.start()); assertEquals(8, matcher.end()); }

12.2. Study Methods

Study methods go through the input String and return a boolean indicating whether or not the pattern is found. Commonly used are matches and lookingAt methods.

The matches and lookingAt methods both attempt to match an input sequence against a pattern. The difference, is that matches requires the entire input sequence to be matched, while lookingAt does not.

Both methods start at the beginning of the input String :

@Test public void whenStudyMethodsWork_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("dogs are friendly"); assertTrue(matcher.lookingAt()); assertFalse(matcher.matches()); }

The matches method will return true in a case like so:

@Test public void whenMatchesStudyMethodWorks_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("dog"); assertTrue(matcher.matches()); }

12.3. Replacement Methods

Replacement methods are useful to replace text in an input string. The common ones are replaceFirst and replaceAll.

The ReplFirst dan ReplAll kaedah menggantikan teks yang sepadan dengan ungkapan biasa yang diberikan. Sebagai nama-nama mereka menunjukkan, replaceFirst menggantikan kejadian pertama, dan replaceAll menggantikan semua kejadian:

@Test public void whenReplaceFirstWorks_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher( "dogs are domestic animals, dogs are friendly"); String newStr = matcher.replaceFirst("cat"); assertEquals( "cats are domestic animals, dogs are friendly", newStr); }

Ganti semua kejadian:

@Test public void whenReplaceAllWorks_thenCorrect() { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher( "dogs are domestic animals, dogs are friendly"); String newStr = matcher.replaceAll("cat"); assertEquals("cats are domestic animals, cats are friendly", newStr); }

The replaceAll kaedah membolehkan kita untuk menggantikan semua perlawanan dengan penggantian yang sama. Sekiranya kami ingin mengganti pertandingan berdasarkan kes, kami memerlukan teknik penggantian token.

13. Kesimpulannya

Dalam artikel ini, kami telah belajar bagaimana menggunakan ungkapan biasa di Java dan juga meneroka ciri-ciri terpenting dari pakej java.util.regex .

Kod sumber penuh untuk projek termasuk semua contoh kod yang digunakan di sini boleh didapati di projek GitHub.