-2

I am looking for a regular expressions pattern that can detect any string in between [ and ] and has .png in it. For example [Anything here with .png is a match] So far I have this: "\[[^>]+]" which detects anything in between [ and ], but I wanted to only include strings that have .png

I am not experienced with Regular Expressions.

If I pass "abc [abc.png] abc [abc] abc" I want to get [abc.png]

If I pass "abc [abc.pngabc] abc [abc] abc" I want to get [abc.pngabc]

Dogahe
  • 1,380
  • 2
  • 20
  • 50
  • 1
    Does this answer your question? [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – jonrsharpe May 21 '20 at 19:20
  • Thanks I will study this if I see I need Regular Expressions extensively in my day to day job. So far I've been able to get by without becoming a regular expressions expert. I'll keep the link handy for future reads. – Dogahe May 21 '20 at 19:33

2 Answers2

1

Here's one approach: \[.*\.png.*\]

\[ escaped left bracket

.* any character, zero or more

\.png literal string with escaped period character

.* any character, zero or more

\] escaped right bracket


To address multiple brackets I'd try looking for "not brackets" instead of any character, like so: \[[^\[]*\.png[^\[]*\]

It's the same pattern as above, but instead of .* we're looking for all non-bracketed characters [^\[].


I find the site regexr.com (no affiliation) does a great job of explaining how regular expression work and provides a playground for testing patterns. Here's an example using this question.

  • Thanks for your answer. Although, I had to remove my acceptance of the answer because I just found that there is a problem with it. In this example: "abc [abc.png] abc [abc] abc", I want the match to be [abc.png] but your answer gives me the match as: [abc.png] abc [abc] – Dogahe May 21 '20 at 20:09
  • Updated question and regexr test suite. – Reticulated Spline May 22 '20 at 14:17
1

if you just want the string between the brackets that ends with .png, try this: (assuming your string always ends in a ".png]")

//this will find a string of any length that ends with .png and is surrounded by []
(?<=\[)\S*\.png(?=\])
\\(?<=...) is called a positive lookbehind and checks if something is adjacent to the left of your match
\\(?=...)Positive lookbehind checks for something to the right

If you would like to include the [] as well, use this: (This uses non square bracket characters as it assumes they are the only thing you can't have in your string or that separate your strings. Fully robust.

\[[^\]\[]*\.png[^\]\[]*\]

Example.

[abc.png] abc [abc] [hey.png]
\\will return
[abc.png] [hey.png]
Johnson
  • 161
  • 9
  • Please read my question and my comment to the other answer. I am clear on what I am looking for. – Dogahe May 21 '20 at 20:29
  • @Dogahe, got you bro, updated my answer, try to be more polite man, goes a long way. Cheers – Johnson May 21 '20 at 20:41
  • Ok, the problem with \[\S*\.png\] is that if I pass: "abc [abc.pngabc] abc [abc] abc" it returns no match. But I want it to return [abc.pngabc] – Dogahe May 21 '20 at 21:03
  • if you pass abc [abc.png] abc [ abcd.png] abc, it doesn't detect the 2nd match. – Dogahe May 22 '20 at 12:32
  • Damn @Dogahe stop drip feeding requirements bro. Just define some constraints about your strings and what might be in them or between them. Are you really expecting whitespace between the brackets, any and all characters? How are the individual strings separated? – Johnson May 22 '20 at 13:37
  • "I found the solution: "\[(.*?.png.*?)\]" – Dogahe" This doesn't work. [Example](https://regex101.com/r/CDbTTE/3) – Johnson May 22 '20 at 13:40