0

Hey so I am trying to get all the substrings that match a Regex in a string as an array.

Example string- "[file/path/name.exe]value" I need to get [file/path/name.exe] as the result.

Few more examples -

  1. "[file/path/name.exe][file/path/name1.exe]val" Result - ["[file/path/name.exe]", "[file/path/name.exe]"]
  2. "[file/path/name1.exe][value][file/path/name2.exe]some_value" Result - ["[file/path/name1.exe]", "[file/path/name2.exe]"]
  3. "[file/path/name1.exe]http:/\[file/path/name2.exe]some_value" Result - ["[file/path/name1.exe]", "[file/path/name2.exe]"]

So basically I need to extract anything that's between brackets [] and is a file path that ends with ".exe"

This is the Regex I tried so far -

let reg = new RegExp(/\[\S+\.exe\]/gi)

This gives the following results - And string.match() function in java script to get the results as an array.

  1. "[file/path/name.exe]value" Result - ["[file/path/name.exe]"]. Works for this case.

  2. But for this case, it doesn't work "[file/path/name.exe][file/path/name1.exe]val" I am getting this - ["[file/path/name.exe][file/path/name1.exe]"] as one element.

  3. And same for this case too "[file/path/name1.exe][value][file/path/name2.exe]some_value" I am getting this - ["[file/path/name1.exe][value][file/path/name2.exe]"] as one string.

So I tried doing negative look ahead like there should be only one opening bracket "[". But that didn't workout. Regex was - let reg = new RegExp(/\[\S+\.exe\]$(?!\[)/gi) but it doesn't work for any case.

I also tried to limit the word ".exe" to one but also didn't work. Regex was-let reg = new RegExp(/\[\S+\(\b\.exe\b){1}\]/gi)

As you can see I am not an expert in Regex. Any pointers would be appreciated. Thanks.

PS: Apologies for any formatting issues. Tried my best on a phone :-).

  • Please visit [help], take [tour] to see what and [ask]. Do some research, ***[search for related topics on SO](https://www.google.com/search?q=regex+find+all+between+square+brackets+site:stackoverflow.com)***; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jul 17 '21 at 05:54
  • 1
    Just find all stuff in square brackets and filter the result on stuff that has .exe https://regex101.com/r/LUVRYK/1 – mplungjan Jul 17 '21 at 05:56
  • https://jsfiddle.net/mplungjan/Lhp6fx19/ – mplungjan Jul 17 '21 at 06:21
  • @mplungjan Yes I did post the Regex that I used and provided examples of what worked and what did not. Not sure why this has been down voted. – Yogesh Sarma Jul 17 '21 at 08:48
  • @mplungjan also I was hoping u would explain the Regex u wrote? Thanks. – Yogesh Sarma Jul 17 '21 at 08:56
  • I did - click the regex101 link – mplungjan Jul 17 '21 at 09:04
  • Downvote possibly due to missing example code - if you next time click [edit], then `[<>]` snippet editor you can make a [mcve] like I did in JSFiddle – mplungjan Jul 17 '21 at 10:00
  • 1
    @mplungjan ya thanks for the explanation couldn't see it in mobile view. – Yogesh Sarma Jul 19 '21 at 05:06
  • @mplungjan sorry to bother u so many times. But can u tell me what is wrong with this regex - /\[\S+(\b\.exe\b){1}\]/gi ? i am checking for any number of word characters with \S+ followed by a group (\b.exe\b) which includes a word ".exe" and it should occur only once- {1} and all of this is in between 2 square brackets -\[ \]. Thanks – Yogesh Sarma Jul 19 '21 at 10:47
  • Sorry I am not currently near a computer- use the regex101 to test your regex – mplungjan Jul 19 '21 at 11:38

0 Answers0