6

2 ways to Split String with Dot (.) in Java using Regular Expression? Examples

 6 months ago
source link: https://javarevisited.blogspot.com/2016/02/2-ways-to-split-string-with-dot-in-java-using-regular-expression.html#axzz8TuRxhcxJ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

2 ways to Split String with Dot (.) in Java using Regular Expression? Examples

You can use the split() method of java.lang.String class to split a string based on the dot. Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot. One more reason for this struggle is the dot being a special character in the regular expression. If you want to split String on the dot you need to escape dot as \\. instead of just passing "." to the split() method. Alternatively, you can also use the regular expression [.] to split the String by a dot in Java. The dot is mostly used to get the file extension as shown in our example.

16596063615762eb9559b523f.png
Video Player is loading.
Loaded: 0.00%

The logic and method are exactly the same as earlier examples of split string on space and split the string on a comma, the only difference is the regular expression. Once you know how to write the regular expression, all these examples will be the same for you. Java's regular expression is inspired by Perl.

A regular expression is often considered as an advanced concept by Java developers and that's the main reason for Java programmers not being comfortable with utilizing the power of regular expression for text processing.

Also, some of the most popular Java books like Head First Java or Core Java Volume 1 don't cover regular expression in good detail; but, as a Java developer, you cannot ignore regular expression.

That's why I highly recommend you to join The Complete Regular Expression course for beginners course on Udemy. It's a great course to learn everything about RegEx you want to know and it's also very affordable, you can buy in just $10 on Udemy sales which happens every now and then.

A regular expression is one of the best and most powerful tools for an experienced developer, whether you are working on Java projects or searching log files for patterns in the UNIX box. Since Java, a regular expression in Perl like learning Java regex also helps to effectively use the find and grep commands.

Splitting String by Dot in Java using Regular Expression

Most of the Java programmers first try the following approach when they need to split the String on dot character:

String textfile = "ReadMe.txt";
String filename = textfile.split(".")[0];
String extension = textfile.split(".")[1];


This will not work because dot (.) is a special character in Java's regular expression to match any single character. The above code will throw java.lang.ArrayIndexOutOfBoundsException: 0 because split() will return an empty array, as shown below:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at StringSplitWithRegEx.main(StringSplitWithRegEx.java:9)


The problem with this code is that "." is a metacharacter if you want to use it literally you need to escape it by using backslash e.g. \\. , though you should remember that to escape dot you just need one backslash i.e \., but in Java since \ backslash also need escaping you need two backslashes or \\, as shown below:

String textfile = "ReadMe.txt";
String filename = textfile.split("\\.")[0];
String extension = textfile.split("\\.")[1];


Alternatively, you can also use the [.] regular expression to split the String by dots in Java, as shown below:

String extension = "minecraft.exe".split("[.]")[1];


The reason [.] works because the dot is inside character class i.e. double brackets []. Only characters ]^-\ have a special meaning inside character classes in Java and dot is not one of them, which means you can use it literally inside character class or [ ].

Though it's good to remember that which characters has special meaning in Java regular expression inside and outside of character class

1) The characters .^$|*+?()[{\ have special meaning outside of character classes.
2) The characters ]^-\ have a special meaning inside of character classes.

If you are an experienced Java developer then you should join these best regular expression courses to master the regex in Java. It will not only teach you basics of regex but also empower you with how to use them effectively.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK