0

I'm looking for help in crafting a regular expression that detects a count of 10 (or more) of the characters '%' or '=' or ':' in a string of any length.

Thus far, I've checked the following sources but couldn't seem to adapt what was posted to my needs:

In addition, I've tried the following expressions myself but they wouldn't match my string. (I've been using "pcretest" to check for matches)

  • /[%=:]{10,}/
  • /[a-zA-Z0-9](:|%|=){10,}/

Can any one assist?

Community
  • 1
  • 1
James
  • 23
  • 1
  • 2
    Welcome to the site - please *do not repost questions*. There is an edit link at the bottom of your existing question which you can use to make changes to it. I have merged both of your questions together so the answers are consolidated in a single place. You may have to re-accept an answer on this question. – BoltClock Dec 27 '11 at 01:27

5 Answers5

2

This regex will match if a string has 10 (or more) of the characters: %=: with any number of other characters in between:

/(?:[^%=:]*[%=:]){10}/

If you need to match the entire string containing at least 10, use this one:

/^(?:[^%=:]*[%=:]){10}[\S\s]*$/

You did not say if they need to be consecutive. This answer assumes no.

ridgerunner
  • 33,777
  • 5
  • 57
  • 69
  • Thanks @ridgerunner - yes, I'm now aware of my bad etiquette. I wanted to delete my original post as it didn't indicate that I'd done any research and didn't think to edit instead. FWIW, your answer helped me out. Thank you. – James Dec 27 '11 at 00:52
  • 1
    @James - No problem. It took me a while to learn the ropes around here too. BTW, you can edit your question (and answers) to your hearts content after you post it. And once you decide on the best answer, you should check it off as the correct one. – ridgerunner Dec 27 '11 at 01:41
  • @BoltClock - Thanks for deleting my duplicate post. (Not quite sure how that happened. Normally I'm pretty careful about stuff like that _D'oh!_) – ridgerunner Dec 27 '11 at 02:03
  • @ridgerunner: Nah, it's not your fault. You answered both questions, so when I merged the questions, both of your answers (and both of someone else's) were consolidated. – BoltClock Dec 27 '11 at 02:14
1

...

how about:

/[%=:]{1000,}/

?

Dan
  • 10,531
  • 2
  • 36
  • 55
  • that didn't match, unfortunately. Would it make any difference if I said it needed to match *any* of those three characters 100 or more times? – James Dec 26 '11 at 23:27
  • 1
    @Fredrik title disagrees with body ("1k" vs "100"). OP should be able to figure it out either way, though. – Matt Ball Dec 26 '11 at 23:27
  • @James how about some simple examples of input that should and shouldn't match? – Matt Ball Dec 26 '11 at 23:28
  • here is an example using pcretest: re> /[%=:]{10,}/ Memory allocation (code space): 45 data> XiyRMMZ988:EyhOL8ztsuh0LRX2mqubbD19wElr:4ew&0JBiRvG3&7xl1NTX:gESAP76hzmYCW9951RNV:d0b=LghUavEfOx44WbgL9cnzIuECXfGdi6YPy3JypRj:PZDn5Ju4PUax:o7jqAZxHX4yxziy:OWm9agGAFdRLR7rAh&cqc:Xl1U8hT5:CYBrZIYcuMnXtsXecNG5yvi5=emc:EDJ3CDJ3hySVsx&gohsvi4pfXoruCxLoUEdI&62WV6q:GH5Xns7f70UpUD0O:OrYGZ:jtEOjaY2LYepBhC:aNQhTPO1dGF6c&z:=3&f6&:=fDuc:ZdX&1Cr1xw4C:&GS&nXiTS4ls=Se5::7A1E No match At the least, given the above test string, you can see that it matches at least 10 of ":", yet I get no match. – James Dec 26 '11 at 23:32
  • 1
    @James, that doesn't belong in a comment anyway. Please edit your question and add the example there. – Alan Moore Dec 27 '11 at 00:17
1
cnt = 0
for each character in string:
    if character is '%' or ':' or '=':
        cnt++

    if cnt >= 10: # or 100 or 1000
        SUCCESS
        break
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • You should probably break out of the loop as soon as `cnt` reaches 10 so as not to examine extra characters. – jwodder Dec 27 '11 at 00:14
0

Calculate the length of your input.

Then have another variable which is the input where you substituted, globally, [%=:]+ for nothing.

Calculate the length of this variable, compare with the length of input: if the difference is 100 or more, you have a match ;)

fge
  • 119,121
  • 33
  • 254
  • 329
0

It will depend on which regular expression engine you are using, but this should work:

/([%:=][^%:=]*){10,}/

Depending on your programming language, you may need to escape the parentheses and braces with a leading backslash (e.g: /\([%:=][^%:=]*\)\{10,\}/).

This will match one of your chosen characters {'%', ':', '='} followed, optionally, by any number of characters not in that set, ten times or more.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247