0

Suppose I have the following possible inputs:

AAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBB
...
zzzzzzzzzzzzzzzzzzzzzzz
...
99999999999999999999999

I'd like to write simple PCRE that verifies whether we are facing this case, ie sequences of a given character 'X' times.

Tzury Bar Yochay
  • 8,798
  • 5
  • 49
  • 73

1 Answers1

3

Let's say X = 10.

If you know the character, it's simple. for instance, for A:

A{10}

If you want to match any character X times, the regex will be:

(.)\1{9}

Explanation: match a character, then try to match the same one 9 more times.

If you want at least X times, use this variant:

(.)\1{9,}

If you just want to validate the whole input is a sequence of the same character, this will do:

^(.)\1*$

Or this, if you additionally want to validate the length:

^(.)\1{9}$
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • You're assuming we know in advance how long the lines should be. The OP doesn't say that. (The OP also doesn't say we *don't*.) – T.J. Crowder Sep 26 '14 at 22:11
  • Well, apparently we know in advance how long the lines are. :-) – T.J. Crowder Sep 26 '14 at 22:12
  • @T.J.Crowder my last example doesn't assume that. I didn't really understand which particular case the OP needs so I just wrote a bunch of them ;) – Lucas Trzesniewski Sep 26 '14 at 22:12
  • The (formerly) last example also doesn't do what I'm pretty sure the question requires: Ensure that the lines are of equal length. – T.J. Crowder Sep 26 '14 at 22:12
  • 1
    @T.J.Crowder in my understanding, OP means each line is a different example input (he says *inputs* in the question). – Lucas Trzesniewski Sep 26 '14 at 22:13
  • Could you add explanations also to the last two expressions like you did for the first (just for educational purposes)? – NoDataDumpNoContribution Sep 29 '14 at 07:31
  • @Trilarion This is basic regex syntax so I didn't explain it... But basically `^` is an anchor that only matches at the start of the string and `$` is an anchor that only matches at the end. OTher than that the regex inside is the same. Oh, and `*` means *zero or more times*. – Lucas Trzesniewski Sep 29 '14 at 07:39
  • Thanks. Yes it's basic syntax but with regex I always get confused how you can assemble basix syntax into complex expressions. Here I learned something. – NoDataDumpNoContribution Sep 29 '14 at 07:43
  • @Trilarion HTH. If you want, [here](http://stackoverflow.com/a/22944075/3764814)'s a regex FAQ that may be helpful (and the first advice about the book is really good). – Lucas Trzesniewski Sep 29 '14 at 07:51