
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java regex program to split a string at every space and punctuation.
The regular expression "[!._,'@?//s]" matches all the punctuation marks and spaces.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main( String args[] ) { String input = "This is!a.sample"text,with punctuation!marks"; Pattern p = Pattern.compile("[!._,'@?//s]"); Matcher m = p.matcher(input); int count = 0; while(m.find()) { count++; } System.out.println("Number of matches: "+count); } }
Output
Number of matches: 8
The split() method of the String class accepts a value representing a regular expression and splits the current string into array of tokens (words), treating the string between the occurrence of two matches as one token.
For example, if you pass single space " " as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.
Therefore, to split a string at every space and punctuation, invoke the split() method on it by passing the above specified regular expression as a parameter.
Example
import java.util.Scanner; import java.util.StringTokenizer; public class RegExample { public static void main( String args[] ) { String regex = "[!._,'@? ]"; System.out.println("Enter a string: "); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); StringTokenizer str = new StringTokenizer(input,regex); while(str.hasMoreTokens()) { System.out.println(str.nextToken()); } } }
Output
Enter a string: This is!a.sample text,with punctuation!marks@and_spaces This is a sample text with punctuation marks and spaces
- Related Articles
- Java regex program to split a string with line endings as delimiter
- Java regex program to add space between a number and word in Java.
- Java program to split and join a string
- Python Program to Take in a String and Replace Every Blank Space with Hyphen
- Java regex program to verify whether a String contains at least one alphanumeric character.
- Java Program to split a string with dot
- How to replace multiple spaces in a string using a single space using Java regex?
- Java Program to split a string using Regular Expression
- How to match a white space equivalent using Java RegEx?
- Python program to split and join a string?
- C# program to split and join a string
- Split string into sentences using regex in PHP
- Posix character classes p{Space} Java regex.
- How to match a non-white space equivalent using Java RegEx?
- Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?

Advertisements