5 Replies - 939 Views - Last Post: 16 January 2012 - 04:26 AM Rate Topic: -----

#1 Dworza   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Basic regex

Posted 15 January 2012 - 01:50 PM

Hello, I want to create regex, that will match numbers...
I've created this:
string str = myRegex; //let it for example be "[8-10]"
Regex isnumber = new Regex(str); /* when the number has 2 or more digits, then the exception is thrown here [x-y] range in reverse order */
matches = isnumber.IsMatch(9);
...
...



can you please tell me, how string in new Regex(str) should look like to be able match numbers from 0 to 999 ?

Is This A Good Question/Topic? 0
  • +

Replies To: Basic regex

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Basic regex

Posted 15 January 2012 - 01:57 PM

You can use the regex pattern... \d{1,3} which will match any number that has 1 to 3 digits... covering 0 to 999. (Note: Would also cover 000 just so you know)

The pattern above says a digit (the \d part aka specifier) which appears 1 to 3 times (the quantifier).

:)

This post has been edited by Martyr2: 15 January 2012 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dworza   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 172
  • Joined: 13-February 11

Re: Basic regex

Posted 15 January 2012 - 02:13 PM

well..I'll need to specify the range..so let's say, that I need a pattern for numbers 34-102
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Basic regex

Posted 15 January 2012 - 02:20 PM

A really good page to check out...

Example: Matching Numeric Ranges with a Regular Expression

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#5 cilaes   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 75
  • Joined: 12-December 11

Re: Basic regex

Posted 15 January 2012 - 04:19 PM

View PostMartyr2, on 15 January 2012 - 02:20 PM, said:

A really good page to check out...

Example: Matching Numeric Ranges with a Regular Expression

Enjoy! :)


For some reason your link was all jacked up for me, so I figured I'd post it as CODE in case they had the same problem.

http://www.regular-expressions.info/numericranges.html


EDIT: seems this displays another bug, it displays fine until the actual [code] renders, it's doing the same thing in your link except that it replaces with safe-codes (lt; gt; etc).

This post has been edited by cilaes: 15 January 2012 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Basic regex

Posted 16 January 2012 - 04:26 AM

The bug you mention regarding the word "expressions" is well-known.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1