Royson
Royson

Reputation: 2901

How to write a regular expression to match a set of values?

I want to check if my string holds following value or not:

For samples:

My condition is

48-57(1 or more time)32(or)10(or)13(then)48(then)32(or)10(or)13(then)111981066060  

Question: How to write regular expression for above condition. Parenthesis indicate occurrence.

Upvotes: 0

Views: 1085

Answers (6)

rsp
rsp

Reputation: 23383

You can start with the following and see if you need to make imprevements

(4[89]|5[0-7])+(32|10|13)48(32|10|13)111981066060

it might be better to split up into fields and check those fields against your business logic instead of relying on a static regexp like the above.

Upvotes: 0

Rubens Farias
Rubens Farias

Reputation: 57996

This this:

(48|49|50|51|52|53|54|55|56|57)+(32|10|13)48(32|10|13)111981066060

EDIT: Just saw Paul answer and this really can be shortened to

(4[89]|5[0-7])+(32|10|13)48(32|10|13)111981066060

Only difference between hos and mine approaches is: he doesn't keeps grouping information (as that isn't required) and I don't care for ignore them.

Upvotes: 0

Russ Cam
Russ Cam

Reputation: 125538

You can test regular expressions in .NET online using Nregex

Upvotes: 0

Paul Creasey
Paul Creasey

Reputation: 28894

Regex regex = new Regex(@"(?:4[89]|5[0-7])+(?:32|10|13)48(?:32|10|13)111981066060");
regex.Match(string);

Didnt test though!

Upvotes: 1

marc_s
marc_s

Reputation: 755541

Check out Expresso - it allows you to build and test your regexs, and creates a C# code snippet or a .NET assembly for you right away. Highly recommended!

Upvotes: 1

Amarghosh
Amarghosh

Reputation: 59471

(4[8-9]|5[0-7])+(32|10|13)48(32|10|13)111981066060

Upvotes: 2

Related Questions