Strings and regex patterns, another hell

This section will provide another example of escaping sequence hell—defining regex patterns as Java strings.

To remove whitespaces (spaces or tabs) between a word character and a period (.), you'll need the following regex (regular expression) pattern:

(w)(s+))[.] 

To store it using a Java string, you'll need the following code:

String patternToMatch = "(\w)(\s+)([\.,])";     // Isn't that a lot 
// to digest? // Does the regex
// pattern has // a single
// backslash, or,
// two of them?

I'll share a quick secret here—when I started working with Java, one of my biggest nightmares was defining patterns as string values. I admit it; I had a difficulty writing patterns, and using the  escape character with the sequences in my patterns made my life miserable.

This is also referred to as leaning toothpick syndrome (LTS)—making string values unreadable by using a lot of backslashes.

Let's put an end to this string misery by using raw string literals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.137.59