Gabriel
Gabriel

Reputation: 42459

Replace number with text using regex

I need to go through a file and replace all instances where an issue is mentioned using the Github convention #xxx (where xxx is the issue number), with a link to the issue using the Markdown format.

So for example, this:

#143, #99

should be converted into this:

[#143](https://github.com/repo/issues/143), [#99](https://github.com/repo/issues/99)

I've gotten as far as to being able to select all the issues with three digits using:

#..[0-9]

but this leaves out the two or one digits issues (ie: #5 or #23)

Is there a way to generalize the above command to select all issues, no matter how many digits they have?

Once this is done, how can I make the replacement to add a link to each issue?

Upvotes: 0

Views: 439

Answers (3)

Amit Joki
Amit Joki

Reputation: 59292

You need the regex #(\d+) and replace with [#$1](https://github.com/repo/issues/$1)

Upvotes: 1

blackmind
blackmind

Reputation: 1296

Try this regex for what you are trying to do

#[0-9]{1,3}

Upvotes: 1

anubhava
anubhava

Reputation: 786329

You should use this regex:

#[0-9]{1,3}

to match a issue # between 1 and 3 digits as [0-9]{1,3} will match a number that is 1 to 3 in length.

You can also use use word boundaries:

#[0-9]+\b

Upvotes: 2

Related Questions