5.How to match word and non word characters.

shalya21 profile image Ayush Shalya ・1 min read

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);
public class Main {
public static void main(String[] args){
RegexSearch obj = new RegexSearch();

Input: Devv.to
Output: Dev

You can view the code here also.

   👍 Have a nice day,Coders. 👍

Posted on by:

shalya21 profile

Ayush Shalya


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


Editor guide