I am trying to create validation so the value passed should be in format as i expect.
For example values can be 40% or 40GB.
I am trying to use开发者_JAVA百科 the regex
(\\d*\\.?\\d*)([MB|GB|TB|PB|%]?)
Is above regex correct?
No it's wrong.
The character class
[xyz]is for matching single character. In fact,[MB|GB|TB|PB|%]means matching a single character which is one ofM,B,|,G,T,Por%. Grouping should be done with(?:...)not[...].((?:MB|GB|TB|PB|%)?)Of course it is better to collect the prefixes of the bytes. Also, I think the unit is mandatory, so the
?should be removed:([MGTP]B|%)The regex matches an empty substring, so anything would pass, e.g. use
\d+(?:\.\d+)?instead.
Together:
(\d+(?:\.\d+)?)([MGTP]B|%)
try this
^\d*([MGTP]B|%)$
DEMO
加载中,请稍侯......
精彩评论