1

I have a string that looks like

/home/user/one.jar:/home/user/two.jar:/home/user/three.jar:/home/user/four.jar

how would I write a regex to match paths containing "two.jar" or "three.jar" so I can filter out these paths later to finally get

home/user/one.jar:/home/user/four.jar?

I tried ^:.*(two|three)\.jar:$ to basically say

  1. Match any string that begins with :
  2. Has any number of characters in between (to match paths like /home/user/
  3. Match by either "two.jar" or "three.jar"
  4. Ends with :

but I don't think I'm approaching this correctly

3
  • in your question it appears you are asking to return 'one' and 'three' but you want the result set to return 'two' and 'four'. Question reads a little confusing Commented Aug 5, 2020 at 14:44
  • I want the result to return "two" and "four" by filtering out "one" and "three" based on the names "one.jar" and "three.jar". Edited the question for clarification Commented Aug 5, 2020 at 14:45
  • Sorry updated the question to clarify. I actually want the regex to return paths containing "two" and "three" so I can filter these out later and get the final path containing only "one" and "four" Commented Aug 5, 2020 at 14:50

2 Answers 2

1

Got it to work with

(^|:)([^:]*(two|three)\.jar)

Sign up to request clarification or add additional context in comments.

Comments

0

Taking this as your problem statement: :/home/user/one.jar:/home/user/two.jar:/home/user/three.jar:/home/user/four.jar

And this as your required solution: home/user/one.jar:/home/user/four.jar

I have written this piece of regex that will help you: (:.+?(?:two|three)\.jar)

This will give you

:home/user/two.jar in Match 1 Group 1

:/home/user/three.jar in Match 2 Group 1

Here is a regex101 link to the solution: https://regex101.com/r/DQMgkg/2

Hope it helped.

2 Comments

This will grab anything in between "one" and "four". What if I wanted to match the same paths but the given string was instead /home/user/zero.jar:/home/user/two.jar:/home/user/one.jar:/home/user/four.jar:/home/user/three.jar? I need to match paths based on specific jar names, no matter how many paths are before/after/between.
Try this regex (:?\/home\/user.?(?|one|four)\.jar) this should do it. Here is the regex101 link: regex101.com/r/DQMgkg/3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.