How to extract all the words from a string include some punctuation marks using regex in Java

1 Answer

0 votes
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class MyClass {
    public static void main(String args[]) {
        String s = "java c c++ c# php python";

        Pattern p = Pattern.compile("[a-zA-Z+#]+"); 
          
        Matcher m = p.matcher(s); 

        while (m.find()) { 
            System.out.println(m.group()); 
        } 
    }
}



/*
run:

java
c
c++
c#
php
python

*/

 



answered Aug 3, 2019 by avibootz
...