Adding an AND clause to a regular expression

I have this simple regular expression,

[\d]{1,5}

which matches any integer from 0 to 99999.

How can I change it so that it does not match 0, but matches 01 and 10, etc.?

I know there is a way to do OR, for example ...

[\d]{1,5}|[^0]{1}

(doesn't make much sense)

Is there a way to do AND?

+3
source share
8 answers

perhaps better with something like:

0 * [1-9] + [\ d] {0.4}

If I'm right, that translates to β€œzero or more zeros,” followed by at least one of the characters included in β€œ1-9,” and then up to four trailing decimal characters "

Mike

+5
source

I think the easiest way is:

[1-9]\d{0,4}

a ^ $, , , 0 * :

^0*[1-9]\d{0,4}$
+3

, . , int , > 0.

, - , .

+2

OR , , :

^ [1-9] $| ^\ {2,5} $

+2

, . :

#!/bin/perl -w

while (<>)
{
    chomp;
    print "OK: $_\n" if m/^(?!0+$)\d{1,6}$/;
}

:

0
00
000
0000
00000
000000
0000001
000001
OK: 000001
101
OK: 101
01
OK: 01
00001
OK: 00001
1000
OK: 1000
101
OK: 101
+2

AND.

^(?=regex1)(?=regex2)(?=regex3).*

Internet Explorer , (?= ) .

http://blog.stevenlevithan.com/archives/regex-lookahead-bug

:

^(?=\d{1,5}$)(?=.*?[1-9]).*
+1

, 2 . 2 ? .

var str = user_string;
if ('0' != str && str.matches(/^\d{1,5}$/) {
    // code for match
}

, 0

var str = user_string;
if (!str.matches(/^0+$/) && str.matches(/^\d{1,5}$/) {
    // code for match
}

, , , .

+1
^([1-9][0-9]{0,4}|[0-9]{,1}[1-9][0-9]{,3}|[0-9]{,2}[1-9][0-9]{,2}|[0-9]{,3}[1-9][0-9]|[0-9]{,4}[1-9])$

, . . , .

0

Source: https://habr.com/ru/post/1698526/


All Articles