c# - How to formulate Regular expression for given example -



c# - How to formulate Regular expression for given example -

i'm not versed regular expressions. i've had utilize them, maybe 1 time every few years, , course of study work. anyways, next question should straight forwards question/answer familiar regular expressions.

i need ensure text entered field follows next format:

x y z

or

x,y,z

or

x y z / <same pattern can repeat unlimited length>

or

x,y,z / <...> // spaces after comma ok

where x, y , z can integers. patterns cannot intermixed, cannot have

x, y, z / x y z / ...

i've tried following

([1-9] [1-9] [1-9])

to x y z part, don't know how include '/' nor ','

any suggestions?

i guess can use

regex r = new regex("^([0-9]+([ ,]|, )[0-9]+(\\2)[0-9]+)( [/] ([0-9]+(\\2)[0-9]+(\\2)[0-9]+)+)*$"); var x1 = r.ismatch("1,2 3"); // false var x2 = r.ismatch("1 3 2 / 1 2 3"); // true var x3 = r.ismatch("1,3,2"); // true var x4 = r.ismatch("1 3 2 / 1"); // false console.writeline((x1 == false) ? "correct" : "error"); console.writeline((x2 == true) ? "correct" : "error"); console.writeline((x3 == true) ? "correct" : "error"); console.writeline((x4 == false) ? "correct" : "error"); console.readline();

spliting in smaller pieces

[0-9]+ matches number, 0. if can't start 0 have alter [ ,] separator allows space or comma \\2 matches same thing sec grouping matched (space or comma)

the sec big parenthesis matches or not more iterations of sequence if started /.

if separator needs same, replace them \\2 (just don't replace first, going match grouping 2).

c# regex

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -