1

I am trying to add the commit message hook to accept the following inputs in Gitlab:

bug 12345
bug #12345
BUG # 12345
Bug#12345
bug12345

The number 12345 should be changed to anything which is either a 5 digit or 6 digit number (not less than that, not greater than that)

It should get started from Bug, BUG or bug.

It should not have any other character than # or space or blank that too just between the numbers and string "Bug/bug/BUG"

I need help with this ASAP.

I have tried to achieve this by :

^(BUG|bug|Bug)\ |#\d+

but this is not giving the desired output. I am taking reference from :

  1. https://regex101.com/
  2. https://github.com/google/re2/wiki/Syntax

Thanks a lot in advance.

Sameer Atharkar
  • 382
  • 1
  • 3
  • 17

1 Answers1

1

You can use

^(BUG|bug|Bug) *(?:# *)?\d{5,6}$

See the regex demo

If you just want to match bug in a case insensitive way, you can write it as (?i)^Bug *(?:# *)?\d{5,6}$.

To match any whitespace, replace the literal spaces in the pattern with a \s construct.

More details:

  • ^ - start of string
  • (BUG|bug|Bug) - BUG, bug, or Bug
  • *(?:# *)? - zero or more spaces followed with an optional sequence of a # followed with zero or more spaces
  • \d{5,6} - five or six digits
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You have some God Level powers @Wiktor Worked like a charm!!!!!!!!!!! – Sameer Atharkar Mar 11 '21 at 10:14
  • Hey @Wiktor what REGEX should be if I want to accept an optional string after space in the end? Like It should accept : 1. bug12345 2. bug12345 updated file1 (If a message is passed it should have a space compulsory) – Sameer Atharkar Mar 12 '21 at 08:54
  • 1
    @SameerAtharkar I think you mean `^(BUG|bug|Bug) *(?:# *)?\d{5,6}(?: .*)?$` – Wiktor Stribiżew Mar 12 '21 at 09:05
  • It should not have a number later.. It should only accept alphabets or sentences but not any number. Like -> bug 12345 updated file one Sorry I wrote 1 previously – Sameer Atharkar Mar 12 '21 at 09:46
  • 1
    @SameerAtharkar `\D` matches any char but a digit. Use `^(BUG|bug|Bug) *(?:# *)?\d{5,6}(?: \D+)?$` then. Or, if there must be a single space between the bug and the text, and to support any whitespace - ``^(BUG|bug|Bug)\s*(?:#\s*)?\d{5,6}(?:\s[^\d\s]\D*)?$``. – Wiktor Stribiżew Mar 12 '21 at 09:48
  • It should not accept something like : bug abcdef bug 123456abcded bug 12345#abcdef but it should accept : bug 12345 abcdef bug#12345 abcdef – Sameer Atharkar Mar 12 '21 at 09:59
  • 1
    @SameerAtharkar `^(BUG|bug|Bug) *(?:# *)?\d{5,6}(?: \D+)?$` works, see [this regex demo](https://regex101.com/r/NUGpcq/2). – Wiktor Stribiżew Mar 12 '21 at 10:09
  • Worked like a charm.... I would really like to know how you became such a pro @Wiktor.. One more thing, Is there any way if someone tries to write or mention another bug then it should be able to display another number? like it should accept: `-> bug 12345 bug 21235 optional msg -> bug 12345 bug#12345 ` – Sameer Atharkar Mar 12 '21 at 10:24
  • 1
    @SameerAtharkar If I get it right, you want to validate and extract a portion of text. You will need PyPi regex module and a solution [like this](https://tio.run/##JYyxDoIwFEV3vuJFh/YhIQHEgahNiImrixsLIpQmFJrSJhrx27GN282951z1Nv00ZusqpJq0Ad3y9hXMcAJNHpZDkmb7HHxKXcxhUkZMYz2AnDkJpOe8EXd2GGRtmp5qQikryvt1cdZSWo4QumILITLKbkfdznYw5@r5yaPDF9FtQFmC6CmoLjtkJIIZA9GBLAIApcVoqIybWhnrbLr5X2wQ1/UH) (`regex.fullmatch(r'((?:BUG|bug|Bug) *(?:# *)?(?P\d{5,6}))(?: (?1))*(?: \D+)?', s).captures("result")`). – Wiktor Stribiżew Mar 12 '21 at 10:38
  • 1
    @SameerAtharkar [This is how I became proficient in regex](https://www.buymeacoffee.com/wstribizew/my-regex-road-so-far). – Wiktor Stribiżew Mar 12 '21 at 11:13
  • That is really nice. How can we make that regex to accept this too @Wiktor? -> https://regex101.com/r/qqbN8v/1 I want it to accept now any value which is starting from 1 to any value which is 6 digit number. so basically I want it to accept anything between 1 to 999999 and it should also accept the character which is ':' Like bug:12345 , bug : 12345 just like it is done for the # – Sameer Atharkar Mar 13 '21 at 11:07
  • Please consider this link: https://regex101.com/r/xtiRBW/1 The previous link was just the playground for me – Sameer Atharkar Mar 13 '21 at 11:15
  • I was able to use that ':' here : ^(BUG|bug|Bug) *(?:#* *)?(:)?( )?\d{5,6}(?: \D+)?$ but not sure how to make it 1 to 6 digit letter but it should not be 0 It should be a valid number from 1 to 99999 and not 0 in the beginning. https://regex101.com/r/Wmyyzc/1 – Sameer Atharkar Mar 13 '21 at 11:26
  • 1
    @SameerAtharkar Maybe `^(BUG|bug|Bug) *(?:#* *)?(:)?( )?[1-9]\d{0,5}(?: \D+)?$` [will do](https://regex101.com/r/Wmyyzc/2). – Wiktor Stribiżew Mar 13 '21 at 13:06
  • Thanks a lot @Wiktor You are really a life saver.. I am wondering how can we make it accept bug 00001 or bug 01 ? The number should be just a non zero.? What will be the case now? – Sameer Atharkar Mar 13 '21 at 15:09
  • 1
    @SameerAtharkar You are changing requirements too fast. Use `^(BUG|bug|Bug) *(?:#* *)?(:)?( )?(?!0+(?![^ ]))\d{0,6}(?: \D+)?$` – Wiktor Stribiżew Mar 13 '21 at 17:41
  • Yes actually we are trying to enhance it. :) All credits goes to you buddy. Can you tell me how can we make it accept something: `bug123 test file2 updated` It should accept a number after the string or between the string but it should not start with a number. example :1. bug12345 updated file 1 2. bug # 123 test1 – Sameer Atharkar Mar 15 '21 at 18:29
  • 1
    @SameerAtharkar Try `^(BUG|bug|Bug) *(?:[#:] *)?(?!0+(?![^ ]))\d{0,6}(?: [^\s\d]\S*)*$`, see [demo](https://regex101.com/r/Wmyyzc/4). – Wiktor Stribiżew Mar 15 '21 at 19:26
  • Can you tell me how I can accept an optional ":" or "::" when there is a space just before the optional message? Like `bug 12345::testmessage1` or `bug#123:testmessage1` and it also should accept `bug123 :: test message` Can you please explain as well your answer? I tried to add (::)? but it was not letting me do the needfuls. https://regex101.com/r/Dfpk4o/1 Check here. Thanks a lot.... – Sameer Atharkar Mar 22 '21 at 08:05
  • 1
    @SameerAtharkar Trying to guess: `^(BUG|bug|Bug) *(?:[#:] *)?[1-9]\d{0,5}(?:(?:[ :]|::).+)?$`? See [demo](https://regex101.com/r/Dfpk4o/2). – Wiktor Stribiżew Mar 22 '21 at 09:44
  • Thanks a lot. This was helpful. I even edited it and made it accept a spacebar in the beginning by making it https://regex101.com/r/hbsmjw/1 like this I am wondering, if there is any possibility that...we can make it in an complete OR condition and accept either this kind of string or else just one "Message one two" like either the string should accept whatever rule we have written or else one complete message like "Message one two" and nothing else. Can we add this kind of condition ? Is it possible via regex? – Sameer Atharkar Mar 22 '21 at 13:45
  • 1
    @SameerAtharkar Maybe you mean https://regex101.com/r/hbsmjw/2. – Wiktor Stribiżew Mar 22 '21 at 13:59