0

so I'm struggling with regex. I'll start with what I want to achieve and then proceed to what I have "so far".

So for example I have commit name lines

merge(#2137): done something
Merge pull request #420 from Example/branch
feat(): done something [#2137JDN]
merge(#690): feat(): done something [#2137JDN]

And I want to grep only by PR ID, or if it's not there then it'd search by that second hash

#2137
#420
#2137JDN
#690

For now I have this regex, but it's not perfect

/(\(|\s|\[)(#\d+|#.+)(\)|\s|\])/g

because it's capturing this

(#2137)
\s#420\s
[#2137JDN]
(#690)[#2137JDN]

How I can improve it to get what I want exactly?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
TheTanadu
  • 554
  • 1
  • 7
  • 33

1 Answers1

1

You can use the #[\dA-Z]+ pattern to grep only hashes.

command | grep -Po "#[\dA-Z]+"

Which returns the all matched strings (in our case - hashes)

#2137
#420
#2137JDN
#690
#2137JDN

Unfortunately, grep does not support non-greedy feature. See this answer.

Artyom Vancyan
  • 5,029
  • 3
  • 12
  • 34
  • 1
    Oh thanks! I mean I didn't decided yet in which script language (or tool like `grep`) I'll use it, I'm just testing logical prove of concept. But seems great. I can each line "compare" to the regex and by that `/(#[\da-zA-Z]+){1}/s` sounds reasonable for what I want to achieve (to get always FIRST match). – TheTanadu May 23 '22 at 17:00