Willy
Willy

Reputation: 507

replace Markdown link with the bare url in special cases

My dataset has an attribute with a link in Markdown format.

For example:

[stackoverflow](http://www.stackoverflow.com)
[GitHub](https://www.github.com)
[See](http://www.microsoft.com)
[More info](http://www.apple.com)
[Even more info](http://www.google.com)

For the last three rows, I want to change the value of the attribute into the url between the brackets. So the resulting dataset should look like:

[stackoverflow](http://www.stackoverflow.com)
[GitHub](https://www.github.com)
http://www.microsoft.com
http://www.apple.com
http://www.google.com

How do I achieve this with a regular expression?

I came up with this:

Search for: ^\[See\]\((.*?)\)$
Replace with: \1

That works fine for the first three rows, but leaves the last two unchanged of course. I can't seem to figure out how to do what I want with one single regular expression. Any suggestions?

Upvotes: 4

Views: 1065

Answers (2)

Lukas
Lukas

Reputation: 164

If your regex engine supports positive look behinds you can try a more generic approach by forcing the regex tho have two preceding lines:

(?<=\n.+\n)\[(.*)\]\((.*?)\)$

and replace with $2 respectively \2

Upvotes: 1

Andy G
Andy G

Reputation: 19367

^\[(See|More info|Even more info)\]\((.*?)\)$

You need an alternation between the 3 versions of text, then the group to replace is now \2. The parentheses are a capture group, but the pipe | chooses from among values.

Upvotes: 3

Related Questions