loading...

3.Matching digits and non digits character.

shalya21 profile image Ayush Shalya ・1 min read

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.👍

Posted on by:

shalya21 profile

Ayush Shalya

@shalya21

I am a B.Tech student and I love to share my knowledge with everyone in a very simple an easy way.

Discussion

markdown guide