-3

I have 2 strings "0000000000ABCDEF" and "1234567890ABCDEF" and I'm trying to find out how to capture "1234567890ABCDEF" using regular expression which has a rule that the first 10 characters must not be all zeroes "0".

Edit:

Thanks for all the useful comments so far.

My apologies if there is any confusion, by capture I mean to match a regular expression with "1234567890ABCDEF". And the same regular expression should not match "0000000000ABCDEF", therefore I felt that the design I'm trying to come up with should contain a rule that checks:

1) the first 10 characters cannot be all zeroes

I tried something like this (?!0{10}).* but it atill matches "0000000000ABCDEF". I guess I'll read up more on regular expressions.

  • 1
    Just to be sure, would a non-completely-regex solution work for you? – Federico klez Culloca Jul 30 '19 at 13:11
  • Also, do you mean the match is a 26-character long strings that contains 10 digits and 6 letters in the range `A-F`? – Federico klez Culloca Jul 30 '19 at 13:12
  • 2
    You could use a [negative lookahead](https://www.regular-expressions.info/lookaround.html), e.g. `(?!0{10})[0-9A-F]{16}` – Aaron Jul 30 '19 at 13:29
  • 1
    @Aaron, first of all, the regex you posted is obviously lookbehind, second, it looks for 16 chars not following 10 zeros, while OP wants to ignore 16 chars where the 10 zeros is part of them – Sharon Ben Asher Jul 30 '19 at 13:38
  • generally speaking, I believe lookahead/lookbehind are not suitable for this case, since these are zero length capture, while OP wishes to capture these chars – Sharon Ben Asher Jul 30 '19 at 13:41
  • 1
    @SharonBenAsher How is that "obviously lookbehind"? A lookbehind starts with `(? – Andreas Jul 30 '19 at 13:41
  • @SharonBenAsher follow the link in my comment and you'll find out most of us call that a negative lookahead. Also https://regex101.com/r/FRr5vq/1 – Aaron Jul 30 '19 at 13:42
  • 1
    Question is unclear. What do you want to capture for the first example (with 10 zeroes)? Also, just to be sure, do you actually want to "capture" something, or do you want to check that input "matches" the pattern, i.e. doesn't start with 10 zeroes? – Andreas Jul 30 '19 at 13:45
  • @Aaron (and Pshemo and Andreas) the lookaround part is placed before the capture. it does not make sense for it to be lookahead and will simply fail to match – Sharon Ben Asher Jul 30 '19 at 13:45
  • 1
    @SharonBenAsher we're not understanding the question in the same way then. The point of my regex is to fail at the start of a 26-wide string it what follows is 10 zeroes, or match the 26 chars otherwise. If OP is using whole-string match it should be perfect, if he's matching in larger strings he'll need anchors (such as `^` or word-boundaries) for better performances and to avoid matching parts of larger numbers – Aaron Jul 30 '19 at 13:47
  • @SharonBenAsher "it does not make sense for it to be lookahead" is not the same as "it isn't lookahead". Anyway it makes prefect sense for case like [Regexp Java for password validation](https://stackoverflow.com/q/3802192) which OP question is quite similar to. – Pshemo Jul 30 '19 at 13:47
  • @Pshemo, the tutorial link provided by Aaron shows this example for negative lookahead `q(?!u)` - the letter `q` not followed by the letter `u`. so what is the meaning (if any) of `(?!u)q` ? – Sharon Ben Asher Jul 30 '19 at 13:51
  • @SharonBenAsher `(?!u)q` will capture `q` and the lookahead is redundant because a `q` is never a `u`. `(?!0).{2}` however would first test whether the following character is not a 0, then capture that character and the following. lookarounds are 0-width match, they just test what follows or precedes without consuming it, if they match the regex carries on, otherwise it fails – Aaron Jul 30 '19 at 13:53
  • @SharonBenAsher `(?!X)Y` can be used in case where Y is more general and you want to eliminate from it subset described by `X`. For instance to find series of two characters except `cd` you can write `[abd-z][a-ce-z]` but also `(?!cd)\w{2}` which IMO is easier to read and understand and maintain if you would like to eliminate more cases like `(?!cd|xy)\w{2}`. – Pshemo Jul 30 '19 at 13:55
  • @Aaron, what's the difference between `(?!0).{2}` and `.{2}(?!0)` ? – Sharon Ben Asher Jul 30 '19 at 13:55
  • 1
    @SharonBenAsher `(?!0).{2}` -> two characters, the first of which isn't a 0. `.{2}(?!0)` -> two characters, that aren't followed by a 0. (I had to edit my previous comment whose start was incorrect btw, I mixed up negative and positive lookahead, sorry about that) – Aaron Jul 30 '19 at 13:56
  • 1
    @SharonBenAsher matching `0123` with `(?!0).{2}` will go as follows : we're at the start of the string. Is the next character a 0? yes, fail and forward in the string (for a search ; for a match that'd be the end of it). Is the next character a 0? no, match two characters, return `12`. If using `g`lobal search, carry on after the last consumed character : is 3 a 0? no. I can't match 2 characters, I've reached the end of the string, done. – Aaron Jul 30 '19 at 13:59
  • @Aaron, thanks for the detailed and thorough explanation. (and for the patience :)) I learned something new today :) – Sharon Ben Asher Jul 30 '19 at 14:02
  • 1
    @SharonBenAsher glad to hear it ! – Aaron Jul 30 '19 at 14:03

1 Answers1

0

You should just be able to use a negative look behind like this: (?<!0{10})ABCDEF

Here is a regex101 for you to see it working: https://regex101.com/r/l7pX8c/1

Sheshank S.
  • 2,482
  • 2
  • 13
  • 33