5.How to match word and non word characters.

Everything you need to know about Regex. (5 Part Series)

1 1.Introduction about Regex
2 2. The dot(.)
3 3.Matching digits and non digits character.
4 4. How to match whitespace and non whitespace characters.
5 5.How to match word and non word characters.

The expression \w matches every word character and \W matches all non word characters.
Word characters include alphanumeric characters ([a-z][A-Z][0-9]) and underscore(_).

e.g:- String Pattern : \\w\\w\\w
Test String : Dev.to

Note: Use \\ instead of \ in Java.

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.tester(“\\w\\w\\w”);
}
}

Input: Devv.to
Output: Dev

You can view the code here also.

    Have a nice day,Coders. 

Enter fullscreen mode Exit fullscreen mode

Everything you need to know about Regex. (5 Part Series)

1 1.Introduction about Regex
2 2. The dot(.)
3 3.Matching digits and non digits character.
4 4. How to match whitespace and non whitespace characters.
5 5.How to match word and non word characters.

原文链接:5.How to match word and non word characters.

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容