Java regular matches numbers and letters, and a combination of spaces

Source: Internet
Author: User

Look for a "combination of numbers and letters and spaces" in an input string, noting that the three appear at the same time, or that only numbers and letters appear without spaces.

For example, input "HG 437 djj third dufwf745, DFN^894DK Big Brother-in-law iphone 6s&&&&74854"

Output:

HG 437 DJJ

dufwf745

894dk

IPhone 6s

The following code can be implemented

1String s = "Hg 437 djj third dufwf745, DFN^894DK Big Brother-in-law iphone 6s&&&&74854";2String pattern = "([a-za-z]+\\s+[0-9]+[a-za-z0-9\\s]*|[ a-za-z]+[0-9]+[a-za-z0-9\\s]*| [0-9]+[a-za-z]+[a-za-z0-9\\s]*| [0-9]+\\s+[a-za-z]+[a-za-z0-9\\s]*] [^a-za-z0-9\\s]*];3Pattern r =Pattern.compile (Pattern);4Matcher m =R.matcher (s);5 6arraylist< String > P =NewArraylist<string>();7          while(M.find ()) {8P.add (M.group (1) );9        } Ten          One          for(String e:p) ASystem.out.println (e);

Java regular matches numbers and letters, and a combination of spaces

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.