Home > front end >  Regex different hashes
Regex different hashes

Time:05-24

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?

CodePudding user response:

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.

  • Related