junni lomo
junni lomo

Reputation: 779

regex to test repeatition of character

I have a specific requirement to check if all the characters in a string of 8 characters contains repeatition of character '0' I was trying to use regular expression 0{8} to validate for all cases to get result as true - but above regular expression will validate only *

0
00
000
0000
00000
000000
0000000
00000000 ->*

Can anyone suggest if i need to change something in my regular expression to validate all the above inputs ? Appreciate your help.

Upvotes: 1

Views: 74

Answers (4)

aar cee
aar cee

Reputation: 239

This should be easy with the following.

a = '000000000'

b = '000001000'

a.strip('0') # returns ''

b.strip('0') # returns '1'

For C++

replace( s.begin(), s.end(), '0', '');

Upvotes: 1

Bohemian
Bohemian

Reputation: 425033

To detect any number of repetitions of 0, simply use:

0+

This matches all your test cases, and of course would match more than 8, but if your input string is max 8 chars, it's OK.

Upvotes: 2

Martin Ender
Martin Ender

Reputation: 44259

{8} requires exactly 8 characters. Use a range:

0{1,8}

Upvotes: 2

Alex1985
Alex1985

Reputation: 658

You should use 0{1,8} to catch all cases you've provided.

Upvotes: 2

Related Questions