You need to escape the dot if you want to split on a literal dot:

String extensionRemoved = filename.split("\\.")[0];

Otherwise you are splitting on the regex ., which means "any character".
Note the double backslash needed to create a single backslash in the regex.


You're getting an ArrayIndexOutOfBoundsException because your input string is just a dot, ie ".", which is an edge case that produces an empty array when split on dot; split(regex) removes all trailing blanks from the result, but since splitting a dot on a dot leaves only two blanks, after trailing blanks are removed you're left with an empty array.

To avoid getting an ArrayIndexOutOfBoundsException for this edge case, use the overloaded version of split(regex, limit), which has a second parameter that is the size limit for the resulting array. When limit is negative, the behaviour of removing trailing blanks from the resulting array is disabled:

".".split("\\.", -1) // returns an array of two blanks, ie ["", ""]

ie, when filename is just a dot ".", calling filename.split("\\.", -1)[0] will return a blank, but calling filename.split("\\.")[0] will throw an ArrayIndexOutOfBoundsException.

Answer from Bohemian on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-split-string-by-dot
Java - Split String by Dot (.) - GeeksforGeeks
July 23, 2025 - Example: The simplest way to split a string by dot is by using the split() method of the String class. Since the dot "." is a special character in regular expressions, it needs to be escaped with double backslashes "\\" to treat it as a literal dot.
Discussions

re.split() not splitting by the assigned variable to split text by?
That shouldn't be inside a "[" group. That's for denoting ranges of characters, meaning that any character within that group will be matched (ie. it's the same as doing: re.split("[0-9.| ]") Ie. it'll split on any character that is a space, a digit, a dot or a |. You presumably want those to be treated as a series of sequences ored together. For that, omit the "[ ]", and escape the "." or it'll be treated as any character. Also, it's kind of long and repetitive (and is the space after 52 intended?). Does the restricted range really matter, or couldn't you just do r" [0-9]+. ". Alternatively, if you do have a list, maybe build it programmatically. Eg: something like "|".join(f" {i}\\. " for i in range(1, 53)) More on reddit.com
๐ŸŒ r/learnpython
6
1
November 12, 2022
split() method in java

the dot is special in regex. If you want to split by dot then you need to escape it. https://www.rexegg.com/regex-quickstart.html

More on reddit.com
๐ŸŒ r/learnjava
8
5
September 25, 2023
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ split-string-by-dot-in-java
Split String by Dot (.) in Java
September 28, 2022 - public class JavaExample{ public static void main(String args[]){ //String that contains dot characters String str = "Text1.Text2.Text3.Text4"; //split the given string by using dot (.) as delimiter String[] strArray = str.split("\\."); //prints substrings after split for(String s: strArray){ System.out.println(s); } } } Output: Related java guides: Split String by Comma ยท
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ split-string-with-dot-in-java
Split String with Dot (.) in Java
December 26, 2024 - The split() method of Java String is used to divide a string into an array of substrings. This method takes a string in the form of a regular expression as a parameter, searches the currently existing string with the given pattern, and splits ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 771194 โ€บ java โ€บ string-split-method-work
Why does my string.split(.) method not work? (Beginning Java forum at Coderanch)
March 19, 2023 - If you look at the Javadoc for ... and has a special use/meaning (other metacharacter are: \^$?|*+(){[ . If you want to split on a literal dot, then you will need to escape it with a \ character....
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 02 โ€บ 2-ways-to-split-string-with-dot-in-java-using-regular-expression.html
2 ways to Split String with Dot (.) in Java using Regular Expression? Examples
March 1, 2024 - 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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java string.split()
Java.String.split() | Baeldung
July 15, 2026 - For example, if we want to split on a dot (.), we need to escape it like this: \\. because the dot (.) is a special character in regular expressions: @Test void whenSplitWithDotDelimiter_thenGetExpectedArray() { String s = "www.example.com"; ...
Find elsewhere
๐ŸŒ
Medium
abdulrahmansmile786.medium.com โ€บ 2-ways-to-split-string-with-dot-in-java-with-examples-da04d32a752b
2 ways to Split String with Dot (.) in Java with examples | by Indian Support | Medium
July 28, 2022 - Unlike comma, colon, or whitespace, ... the regular expression. If you want to split String on the dot you need to escape dot as split a String by dot....
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_string_split.asp
Java String split() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... String myStr = "Split a string by spaces, and also punctuation."; String regex = "[,\\.\\s]"; String[] myArray = myStr.split(regex); for (String s : myArray) { System.out.println(s); }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ java-program-to-split-a-string-with-dot
Java Program to split a string with dot
String str = "Java is a programming language. James Gosling developed it."; We will now see how to split a string using the split() method. Include the delimiter as the parameter. ... Above, we have split the string with dot as you can see under the split methods parameter.
๐ŸŒ
Blogger
self-learning-java-tutorial.blogspot.com โ€บ 2022 โ€บ 05 โ€บ how-to-split-string-by-dot-in-java.html
Programming for beginners: How to split a string by dot (.) in Java?
Since โ€˜.โ€™ is a regular expression pre defined character class, we need to explicitly escape it using a backslash while splitting by dot. ... package com.sample.app.strings; public class SplitByDotUsingRegularExpressions { public static void main(String args[]) { String str = "hello.world.how.are.you"; String[] tokens1 = str.split("\\."); String[] tokens2 = str.split("[.]"); for (String token : tokens1) { System.out.println(token); } System.out.println("\n\n"); for (String token : tokens2) { System.out.println(token); } } } ... package com.sample.app.strings; import java.util.ArrayList; imp
๐ŸŒ
Go Packages
pkg.go.dev โ€บ strings
strings package - strings - Go Packages
FieldsFuncSeq returns an iterator over substrings of s split around runs of Unicode code points satisfying f(c). The iterator yields the same strings that would be returned by FieldsFunc(s), but without constructing the slice.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ re.split() not splitting by the assigned variable to split text by?
r/learnpython on Reddit: re.split() not splitting by the assigned variable to split text by?
November 12, 2022 -

The text I need to split is numbered with entries 1-3 and so I am trying to split the text and get each entry as a separate item in a list:

from bs4 import BeautifulSoup
import requests
import re
page = requests.get("http://classics.mit.edu/Epictetus/epicench.1b.txt")
text = BeautifulSoup(page.text, "html.parser")
strtext = str(text)
print(text)
g = re.split("[ 1. | 2. | 3. | 4. | 5. | 6. | 7. | 8. | 9. | 10. | 11. | 12. | 13. | 14. | 15. | 16. | 17. | 18. | 19. | 20. | 21. | 22. | 23. | 24. | 25. | 26. | 27. | 28. | 29. | 30. | 31. | 32. | 33. | 34. | 35. | 36. | 37. | 38. | 39. | 40. | 41. | 42. | 43. | 44. | 45. | 46. | 47. | 48. | 49. | 50. | 51. | 52 .]", strtext)
print(g)

However, all it returns is the entire text split by whitespace.

๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_strings_specchars.asp
Java Strings - Special Characters
Because strings must be written within quotes, Java will misunderstand this string, and generate an error.
๐ŸŒ
Code2care
code2care.org โ€บ home โ€บ java โ€บ how to split on string in java with regular expressions by dot.
How to Split on String in Java with Regular Expressions by Dot. | Code2care
July 7, 2023 - package org.code2care.example; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaSplitOnDotStringExample2 { public static void main(String[] args) { String sentence = "1.2.3.4.5.6.7.8"; Pattern pattern = Pattern.compile("(\\d+)"); Matcher matcher = pattern.matcher(sentence); //Print them out while (matcher.find()) { String splittedString = matcher.group(1); System.out.print(splittedString+"\t"); } } } Output:
๐ŸŒ
LightNode
go.lightnode.com โ€บ tech โ€บ java-string-split
Mastering Java String Split: Essential Techniques for Efficient Text Processing
April 15, 2025 - The double backslash (\\) is necessary ... one in Java string literals, and the resulting single backslash escapes the dot in the regex pattern. Let's dive deeper into some sophisticated applications of the split() method that can solve common ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 405076 โ€บ java โ€บ Splitting-String
Splitting String at . (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums ยท this forum made possible by our volunteer staff, including ... ... Hi, I'd like to seperate a String containing dots (".") with the split method, this method uses regular expressions.
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ split-string-method-in-java
Split Method in Java: How to split a String using split() Method | Edureka
November 28, 2019 - The String class in Java offers a split() method that can be used to split a string into an array of String objects based on delimiters that match a regular expression. For instance, given the following string: ... More accurately, that expression will break the string into sub-strings wherever the sub-strings are separated by delimiter characters.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-split-a-string-by-character
Java - Split a String by Character - GeeksforGeeks
July 23, 2025 - Explanation: In the above example, the StringTokenizer splits the string into tokens based on the specified delimiter and provides methods like nextToken() to retrieve them. Java 8 introduced streams that offers a functional approach for splitting and processing strings.