简体   繁体   中英

Regex match on specific symbol followed by number and letters

I want to match on a format like the following:

1d2h3m4s5z
51d344h33m44s55z
>12d3h
<12m5s
>12h32s
12s4z
12z

The following should return false/null:

h2m
2g3m4s
455d89m45c
c9m8s

so first character has to be < or > or '' . Then followed by a format like 1d2h3m4s5z but it should return a match if it's just 1d or 1h or >1m3s .

the idea is that these dhmsz represents day , hour , min , second , z

This is what I have, ^[><]\\d+[dhmsz]\\d+[dhmsz]\\d+[dhmsz]\\d+[dhmsz]\\d+[dhmsz]$ https://regex101.com/r/tO2oF1/6 but it's not fully correct. Only <2d2h2m2s2z or >2d2h2m2s2z works.

(double)negative-look-behind excludes prefixes: (?<![^><])

match the stuff (\\d+[dhmsz]){1,5}

negative-look-ahead exclude suffix: (?![^dhmsz])

all together it's (?<![^><])(\\d+[dhmsz]){1,5}(?![^dhmsz])

that should match all your cases

Is 344h a valid input ?

Anyway:

^[><]+\d*[dhmsz]+\d*[dhmsz]+\d*[dhmsz]+\d*[dhmsz]+\d*[dhmsz]+$    

Will work with any length integers. And:

\d{,2}

For integers up to 2 digits long.

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