-1

I need a Regex which matches a string of uppercase characters of exactly 48 characters length and mandatory number 0 in it (1 or more times).

I would like to use it to filter these strings.

For example:

quick brown fox AUSGKAJDGAYGDKJAS0GDKJAGDJKASGDKJASGDKYQGWUDVAS0 jumped over the fence

laszlo
  • 494
  • 4
  • 18
  • Most dialects support `[A-Z]{48}0*`, did you really not google before asking? See also the [tag description](https://stackoverflow.com/tags/regex/info) which tells you what you need to include in your question. Also, 0 is not mandatory if zero repetitions is acceptable. – tripleee Nov 11 '17 at 17:17
  • No, that doesn't work. This matches 48 chars without 0. I need it to match if string _contains_ **0** – laszlo Nov 11 '17 at 17:21
  • Then you don't mean "zero or more" really. Please review the tag guidance and [edit] your question so it's answerable. – tripleee Nov 11 '17 at 17:22
  • Maybe you mean `[A-Z0]{48}` then? – tripleee Nov 11 '17 at 17:23
  • Fixed it, I am sorry. I obviously meant **1 or more times** – laszlo Nov 11 '17 at 17:25
  • ```[A-Z0]{48}``` Matches with or without 0, sadly – laszlo Nov 11 '17 at 17:27
  • 2
    Try this [`(?=^[A-Z0]{48}$)([A-Z0]*0[A-Z0]*)`](https://regex101.com/r/xnqXsD/1). Edit : Nevermind, you want to match this string inside a sentence, gonna fix this. – Paul-Etienne Nov 11 '17 at 17:35
  • Thanks Paul! Works very well. Please post this as answer so I can mark it as accepted answer. EDIT: sorry I didn't check if it matches inside a sentence. – laszlo Nov 11 '17 at 17:39
  • There's a ton of questions like this already, what have you tried and where are you stuck? Try e.g. something along the lines of https://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper or generally any password validation regex question (though using regex for that is usually not a good idea). – tripleee Nov 11 '17 at 17:43

1 Answers1

0

This regex matches your desired pattern :

(?=\b.{48}\b)[A-Z0]*0[A-Z0]*

See this regex demo.

Explanation :

  • The first part of the regex (?=\b.{48}\b) ensures that the full match will be of exactly 48 characters
  • The second part [A-Z0]*0[A-Z0]* is the actual pattern, matching any uppercase character or 0 and making sure at least one 0 is inside.
Paul-Etienne
  • 796
  • 9
  • 23
  • Hey Paul, I would like to use it also in Google Docs, and I just learned their Regex (RE2) doesn't support positive lookaheads. I searched for replacement for re2 but it seems it's not possible. Can you think of any other solution without usage of positive lookaheads? Thanks – laszlo Nov 11 '17 at 18:41
  • I can't think of a way of doing it without positive lookahead, sorry :x – Paul-Etienne Nov 12 '17 at 10:55