Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex Metacharacters

Tags:

java

string

regex

I found this thread and one of users on it posted the following line of code:

String[] digits2 = number.split("(?<=.)");

I have consulted a couple of sources- like 1 and 2-to decipher what this code mean but I can't figure it out. Can anybody explain what the argument in the split() method means?

Edit: To anyone who has the same question as I had, here's another helpful link

like image 868
Haque1 Avatar asked May 20 '13 02:05

Haque1


People also ask

What is metacharacters in regular expression?

A metacharacter is a character that has a special meaning during pattern processing. You use metacharacters in regular expressions to define the search criteria and any text manipulations. Search string metacharacters are different from replacement string metacharacters.

What is the use of \\ in Java?

The backslash \ is an escape character in Java Strings. That means backslash has a predefined meaning in Java. You have to use double backslash \\ to define a single backslash.

How do you escape a character in regex Java?

To escape a metacharacter you use the Java regular expression escape character - the backslash character. Escaping a character means preceding it with the backslash character. For instance, like this: \.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.


1 Answers

This is a positive lookbehind. The overall expression means "after any character, but without capturing anything". Essentially, if the string looks like

ABC

then the matches would occur at |, between the characters.

A|B|C|
like image 180
Sergey Kalinichenko Avatar answered Oct 21 '22 23:10

Sergey Kalinichenko