Regex Match between brackets (...)
Asked Answered
D

2

6

I'm trying to grab 2 items from a simple line.

[Title](Description)

EDIT: actually a url looking to display called it description because i want it displayed not actually parsed.

[Trivium](https://www.youtube.com/user/trivium)

Grabbing between the brackets (...) doesn't seem to work at all for me. I've googled and found several variations with no luck, Thanks in advance :)

EDIT:

Tried the following:

[(.+?)]\((.*)\)
[(.+?)]\([^\(\r\n]*\)
[(.+?)]((.+?))

and a cpl more I cant find again

Detraction answered 29/10, 2015 at 2:40 Comment(5)
Please show us what you've tried up to this point.Chekhov
Did you remember to escape each bracket?Mcalpine
I am not smart with this and dont know what escape means sorry trying to learn stillDetraction
It might help if you also let us know what programming language you are using.Helfrich
xkcd.com/1171Radioactivity
H
11

The first regex you listed almost has it right. Try using this regex instead:

\[.+?\]\((.*)\)

As @PM 77-1 pointed out, you need to escape the brackets by placing a backslash in front of them. The reason for this is that brackets are special regex metacharacters, or characters which have a special meaning. Brackets tell the regex engine to look for classes of characters contained inside of it.

Your original regex [(.+?)]\((.*)\) is actually doing this:

[(.+?)]   match a period '.' 1 or more times
\((.*)\)  match (anything), i.e. anything contained in parentheses

So this regex would match .....(stuff) but would not match [Title](Description), the latter which is what you really want.

Here is a link where you can test out the working regex:

Regex 101

Helfrich answered 29/10, 2015 at 2:46 Comment(4)
Bare with me im testing and it seems like its grabbing all the info after the bracket closes as wellDetraction
\[.+?\]\((.*)\) Try using this regex if you just want to extract what is in between the parentheses. Do you want to extract just the word Description from the example [Title](Description) ?Helfrich
No its not actually the word description. Its a url that i want displayed begins with http:// if that helps anyDetraction
Update your OP with the URL you are trying to match, and I'll adjust my answer. For future reference, give us the full text you are trying to match in the beginning, don't hold it for later.Helfrich
F
0

You can use

\[([^][]*)]\(([^()]*)\)

if your regex engine is PCRE-like. See this regex demo.

In JavaScript, if you use the regex inside pattern attribute, or you plan to use Unicode category classes in the pattern and thus use u or v flag implicitly, you need to escape the pattern a little bit more:

\[([^\]\[]*)\]\(([^\(\)]*)\)

See this regex demo.

Details:

  • \[([^][]*)] - matches a part of text between [ and ] that contains no other [ and ], capturing the text inside the brackets into Group 1:
    • \[ - a [ char
    • ([^][]*) - Capturing group 1 that matches zero or more chars other than [ and ]
    • ] - a ] char
  • \(([^()]*)\) - then matches text between ( and ) that has no other ( and ) inside while capturing the text inside parentheses into Group 2:
    • \( - a ( char
    • ([^()]*) - Capturing group 2 that matches zero or more chars other than ( and )
    • \) - a ) char.
Foggia answered 16/10, 2024 at 6:45 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.