DEV Community

Ayush Shalya
Ayush Shalya

Posted on

3.Matching digits and non digits character.

The expression \d matches any digit[0-9] character and expression \D matches every non-digit character.

Note :- For java use \\d or \\D instead of \d or\D.

e.g:- Regex Pattern : \\d\\d\\d\\d
Test String : dev community @12456
Explanation : Here \\d expression which is written four times will match
any four digits character.

Sample code in Java:

import java.io.;
import java.util.Scanner;
import java.util.regex.
;
class RegexSearch{
public void tester(String Regex_Pattern){
Scanner sc = new Scanner(System.in);
String Test_String = sc.nextLine();
Pattern p = Pattern.compile(Regex_Pattern);
Matcher m = p.matcher(Test_String);
while(m.find()){
System.out.print(m.group());
}
}
}
public class Main{
public static void main(String[] args){
RegexSearch obj = new RegexSearch();
obj.Pattern("\\d\\d\\d\\d\\D");
}
}

Input:- dev community@12456dev
Output:- 1245d

You can view the code here also

Do read my previous blog on REGEX.


    👍 Have a nice day,Coders.👍
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)