How to match string formate in Regex c#

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to match string formate in Regex c#



I am passing a correct string formate but its not return true.


string dimensionsString= "13.5 inches high x 11.42 inches wide x 16.26 inches deep";
// or 10.1 x 12.5 x 30.9 inches
// or 10.1 x 12.5 x 30.9 inches ; 3.2 pounds

Regex rgxFormat = new Regex(@"^([0-9.]+) ([a-z]+) x ([0-9.]+) ([a-z]+) x ([0-9.]+) ([a-z]+)( ; ([0-9.]+) ([a-z]+))?$");
if (rgxFormat.IsMatch(dimensionsString))

//match



I can't understand whats wrong with code ?





Your regex does not match several words after the number. See regex101.com/r/q8XJIB/2 where I just doubled each " ([a-z]+)". However, what are the pattern requirements? Is the number of words fixed?
– Wiktor Stribiżew
20 mins ago



" ([a-z]+)"





@Wiktor Stribiżew there is 10.1 x 12.5 x 30.9 this pattern are fixed but, some time added string after number (2 word or more than 2 also possible).You suggestion is correct for 2 word string but if come more than ? like "13.5 inches high x 11.42 inches wide test x 16.26 inches deep";
– Anup Patil
7 mins ago





Replace them with (.*?). Try ^([0-9.]+) (.*?) x ([0-9.]+) (.*?) x ([0-9.]+) (.*?)( ; ([0-9.]+) (.*))?$, see this demo.
– Wiktor Stribiżew
6 mins ago



(.*?)


^([0-9.]+) (.*?) x ([0-9.]+) (.*?) x ([0-9.]+) (.*?)( ; ([0-9.]+) (.*))?$





@Wiktor Stribiżew Thanks . its working perfectly fine :-)
– Anup Patil
1 min ago




1 Answer
1



Your pattern only accounts for single words after the numbers. Allow any number of symbols there to fix the pattern:


^([0-9.]+) (.*?) x ([0-9.]+) (.*?) x ([0-9.]+) (.*?)( ; ([0-9.]+) (.*))?$



See the regex demo.



Replace regular spaces with s to match any kind of whitespace if necessary.


s






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard