- Java 9 Regular Expressions
- Anubhava Srivastava
- 235字
- 2021-07-02 18:58:35
Examples using quantifiers
Let's look at few examples to understand these basic quantifiers better.
Which regex pattern should be used to match a two-digit year or a four-digit year?
\d{2}|\d{4}
Which regex pattern should be used to match a signed decimal number? The pattern should also match a signed integer number:
^[+-]?\d*\.?\d+$
Here is the breakup of the preceding regex pattern:
- The ^ and $ symbols are the start/end anchors
- The [+-]? pattern makes either the + sign or the - sign (optional because of ?) at the start
- The \d* pattern matches zero or more digits
- The \.? pattern matches an optional dot (.) literally
- The \d+ pattern matches one or more digits
The preceding regex will match all of these inputs:
- .45
- 123789
- 5
- 123.45
- +67.66
- -987.34
What would be the regex to match a number that is at least 10 but not more than 9999?
^\d{2,4}$
Since we have a minimum of two digits, 10 is the smallest match, whereas the maximum number of digits allowed is four, and hence, 9999 is the highest match.
What is the regex for an input that has seven digits and that can have + or - at the start?
^[+-]?\d{7}$
The [+-]? pattern makes it an optional match at the start before we match the seven digits using \d{7}.
- Learning ROS for Robotics Programming(Second Edition)
- Visual C++串口通信開發(fā)入門與編程實(shí)踐
- PostgreSQL Cookbook
- Mastering matplotlib
- UI智能化與前端智能化:工程技術(shù)、實(shí)現(xiàn)方法與編程思想
- 網(wǎng)店設(shè)計(jì)看這本就夠了
- 軟件工程
- 深入理解Elasticsearch(原書第3版)
- 從零開始學(xué)C語言
- The Professional ScrumMaster’s Handbook
- Learning VMware vSphere
- Java程序設(shè)計(jì)教程
- WCF技術(shù)剖析(卷1)
- 編程的原則:改善代碼質(zhì)量的101個(gè)方法
- Python網(wǎng)絡(luò)爬蟲從入門到實(shí)踐