简体   繁体   中英

PERL regex for discord id that ends after 4th digit

I tried using this which appears to work..

^.*#[0-9]{4}$

test#1234

Except this also is accepted as valid input.

test#1234 #1234

How do I go about making it so that after the 4 digits no other characters will be accepted without hindering the initial match?

Thanks in advance for the help.

I'd use:

$string =~ /^\D+#\d{4}$/

Where \\D stands for any character that is not a digit.

One approach would be to use a negative lookahead which asserts that a #1234 pattern does not occur after matching the first one:

^[^#]*#[0-9]{4}(?!.*#[0-9]{4}).*$

Regex101

Perl demo

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