
- 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 program to remove all numbers in a string except "1" and "2"?
The regular expression "(?<!\d)digit(?!\d)" matches the digit specified.
The replaceAll() method accepts two strings: a regular expression pattern and, the replacement string and replaces the pattern with the specified string.
Therefore, to remove all numbers in a string except 1 and 2, replace the regular expressions 1 and 2 with one and two respectively and replace all the other digits with an empty string.
Example
import java.util.Scanner; public class RegexExample { public static void main(String args[]) { //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression to match the digit 1 String regex1 = "(?<!\d)1(?!\d)"; //Regular expression to match the digit 2 String regex2 = "(?<!\d)2(?!\d)"; //Replacing all space characters with single space String result = input.replaceAll(regex1, "one") .replaceAll(regex2, "two") .replaceAll("\s*\d+", ""); System.out.print("Result: "+result); } }
Output
Enter a String sample 1 2 3 4 5 6 Result: sample one two
- Related Articles
- C++ Program to Remove all Characters in a String Except Alphabets
- Python - Remove all characters except letters and numbers
- Java Program to Remove All Whitespaces from a String
- Remove all except the first character of a string in MySQL?
- Java Program to remove all white spaces from a String.
- Java program to remove all the white spaces from a given string
- Golang program to remove all whitespaces from a string
- Java Program to Display All Prime Numbers from 1 to N
- Java Program to remove all elements from a set in Java
- How to remove all whitespace from String in Java?
- How to remove characters except digits from string in Python?
- Java Program to remove leading and trailing whitespace from a string
- How to remove all objects except one or few in R?
- How to remove all documents from a collection except a single document in MongoDB?
- Remove all non-alphabetical characters of a String in Java?

Advertisements