简体   繁体   中英

Using regex to find certain range

I'm trying to use regex to find a range between 1-31. This is what i have so far but its not working. Also could someone please explain to me how the regex formatting works?

1[0-3]?|2[0-9]|[1-9]

像这样分别考虑1-9、10-29和30-31情况。

[1-9]|[12][0-9]|3[01]

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski

Here's the verbose version, which also throws up two other useful functions:

... {
   static public int asInt(String s, int dflt) {
        try {
          return parseInt(s, 10);
        } catch (NumberFormatException nfe) {
          return dflt;
        }
   }

   static public boolean isInRange(int n, int mn, int mx) {
       return (n >= mn) && (n <= mx);
   }

   static public boolean isBetween1and31(String s) {
       return isInRange(asInt(s, 0), 1, 31);
   }
}
([1-9]|[12][0-9]|3[01])

http://gamon.webfactional.com/regexnumericrangegenerator/

This would be enough to generate a regex b/wa range.

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