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 the regex
(\\d*\\.?\\d*)([MB|GB|TB|PB|%]?) Is above regex correct?
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 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 of M, B, |, G, T, P or %. 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|%) (\d+(?:\.\d+)?)([MGTP]?B|%) for 40B (and it matches the possibly invalid 4.5B.)(\d+(?:\.\d+)?)([MGTP]?B|%)?. You know what ? means right?(\d+(?:\.\d+)?)([MGTP]?B|%|AS)?, and please do learn regex properly. All the above requests can be solved by simply adding more alternations. I will stop answering comments here for the same pattern.)