How can I use regular expressions to match consecutive numbers?

Source: Internet
Author: User
How can I use regular expressions to match consecutive numbers? Regular number

① $ Number = 1234784;
The result of regular expression matching is: 1234.

② $ Number = 389456723;
The result of regular expression matching is: 4567.


Reply to discussion (solution)

Continuous numbers cannot be matched only by regular expressions.

Continuous numbers cannot be matched only by regular expressions.
What should I do?

Is the first 78 not continuous? Is the second 89 and 23 not continuous? Why not?

Is "386387" continuous? Do you want to match?

① $ Number = 1234784;
The result of regular expression matching is: 1234.

② $ Number = 389456723;
The result of regular expression matching is: 4567.

Does it seem to match four consecutive digits? If yes, there are several situations:

$ S = 389456723;

// Four consecutive digits
$ R = '(1234 | 2345 | 3456 | 4567 | 5678 | 6789 )';
Preg_match_all ('/'. $ r. '/', $ s, $ m );

Print_r ($ m [0]);


// More than three consecutive digits
$ R = '(1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 123 | 234 | 345 | 456 | 567 | 678 | 789 )';
Preg_match_all ('/'. $ r. '/', $ s, $ m );

Print_r ($ m [0]);

// If there are more than two characters, you don't need to demonstrate it.

Does it seem to match four consecutive digits? If yes, there are several situations:

$ S = 389456723;

// Four consecutive digits
$ R = '(1234 | 2345 | 3456 | 4567 | 5678 | 6789 )';
Preg_match_all ('/'. $ r. '/', $ s, $ m );

Print_r ($ m [0]);


// More than three consecutive digits
$ R = '(1234 | 2345 | 3456 | 4567 | 5678 | 6789 | 123 | 234 | 345 | 456 | 567 | 678 | 789 )';
Preg_match_all ('/'. $ r. '/', $ s, $ m );

Print_r ($ m [0]);

// If there are more than two characters, you don't need to demonstrate it! Good!

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.