1

As an example I have string that is Banjo.ald:Mandolin.ald:Guitar.tst and I am trying to find the regex that will return the string Guitar. In the example above I have two groups preceding the text I am interested in (i.e. Banjo.ald:Mandolin.ald:) but in reality there will be 1 to N of these groups where a group is defined as Banjo.ald:

Let me know if you need more clarity on what I am looking for.

Janus
  • 13
  • 2
  • I need more clarity on what you are looking for. – Casimir et Hippolyte Mar 03 '17 at 02:24
  • @CasimiretHippolyte I think he justs wants `Guitar`.tst if I read correctly. – Tim Biegeleisen Mar 03 '17 at 02:25
  • In the example above I am looking to return Guitar, so I want to drop everything before the last colon (including the last colon itself) and I want to keep everything else before the .tst (not including the .tst). The string will always end with :Guitar.tst, but I dont know how many other something.ald: sections will precede it. Let me know if you need more clarification. – Janus Mar 03 '17 at 02:31

1 Answers1

1

You can use a greedy regex which will consume everything until the last colon. Then capture the word that follows, everything up until the .tst:

^.*:(.*).tst$

Regex101

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I tried using this in regexr as a test but it returns the entire string, possible I am doing something wrong as I am not very familiar with regexr – Janus Mar 03 '17 at 02:35
  • In the demo I gave above it worked, and I also believe this would work in Java and most languages. If you can give me a link to your demo, maybe I can have a look. – Tim Biegeleisen Mar 03 '17 at 02:37
  • Thank you for the link to Regex101, that is very helpful. I realized that the reason I was getting a different response is that my example is flawed. I have spaces in my string: Ban jo.ald:Man dolin.ald:Gui tar.tst – Janus Mar 03 '17 at 02:41
  • Actually, spaces should not matter, but in this example you would end up capturing `Gui tar` instead of `Guitar`. – Tim Biegeleisen Mar 03 '17 at 02:42
  • I stand corrected, the regex engine I am using in my application is not acting the same way as the regex101 site you linked. My suspicion is that where the regex101 site shows the full match as well as the first group, my regex engine is only returning the full match. – Janus Mar 03 '17 at 02:48