简体   繁体   中英

how to get the match strings(either special characters(like +-=.etc) or string(a-z letters) on the given values.

i have here a pure java code, this one gives me an output of "Special Characters are: java.util.regex.Matcher[pattern=[\\p{Punct}&&[^_]]+ region=0,8 lastmatch=&&=+-]" as i compile & run, but i want only to have an output of "Special Characters are: &&=+-" based on my given value. and also the same with strings as "strings are: sam" but dont know how to do this, pls help... i used netbeans ide

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

     class SubstringsOfAString
     {
      public static void main(String args[])
         {

        String sample = "sam&&=+-";
        Pattern p = Pattern.compile("[\\p{Punct}&&[^_]]+");
         Matcher m = p.matcher(sample);

       boolean b = m.find();

       if(b == true)
       {
         System.out.println("Special Characters are: " + m );

         }

       else {
      System.out.println("Nothing" );
       }

            }
     }

Replace the System.out with

         System.out.println("Special Characters are: " + m.group());

In your solution, you are printing whole Matcher object, not the match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM