• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex doubt  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to garantee that a string has only the digit:
1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
I'm doing the following (and i guess it works properly):

But i'm sure there's a better way
i tried
but this one doesnt work...
Can someone help me?
thanks in advance
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Intermediate forum...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but this one doesnt work...
Can someone help me?


It might help others to help you if you were describe what "this one doesn't work" means.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to filter a string, so that my string only has or a
1 or 2 or .. . or 9: no zeros
my second regex doesnt do the job, while the first does, though i guess there's a better way to write the first one
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[123456789] can be abbreviated as [1-9]. Inside a character class [...] you can put a dash "-" between any two characters to represent a range.

\d{1}[^0] will match a single digit (\d{1}) followed by any non-zero character ([^0]) like 3, i, / or Q.

Note that there is no way to exclude one or more characters from an existing character class shorthand like \d or \s. If you want all digits except 3 and 5 you need to use [0-246-9]. As you can see, ranges can be combined in character classes just like other characters. To match - or ], make it the first character in the class.

[1-9] is what you you're after.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[1-9] is what you're after.

You'r right!
thanks a lot
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!