简体   繁体   中英

How to validate number.number.number;number this format in Reg ex

My string should contains in this format number.number.number;number ex:15.2.63;4 How to validate this format in Reg ex. I have done in normal way used contains, spilt etc. But lines of code increased. May I know how do it in reg ex?

You can go with this:

^\d+[.]\d+[.]\d+;\d+$

With a liveDemo

Many ways to do it, here using PCRE:

laptop:~$ echo "12.34.56;7" | perl -ne 'print $_ if (/^\d+\.\d+\.\d+;\d+$/);'

12.34.56;7

laptop:~$ echo "12a.34b.56c;7" | perl -ne 'print $_ if (/\d+\.\d+\.\d+;\d+/);'

laptop:~$ echo "12.34.56;7" | perl -ne 'print $_ if (/^(\d+\.){2}\d+;\d+$/);'

12.34.56;7

If you know the exact length of each part, you can also fix it. For example \\d{2}. will match 11. but won't match 123.

The above answer group dot into bracket ([.]) this is useless for a single character. But if you delimiter may vary, you can use, for example [.;-] to allow . ; and - as a delimiter.

Try this..

^[1-9][\.\d]*[1-9][\.\d]*[1-9][\.\d]*[1-9][\;\d]?$

Hope this helps...

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