4

I'm not very experienced in Regex. Can you tell me how to get a string value from between two strings?

The subject will always be in this format : //subject/some_other_stuff

I need to get the string found between // and /.

For example:

Full String = //Manhattan/Project

Output = Manhattan

Any help will be very much appreciated.

3 Answers 3

5

You can use a negated character class and reference capturing group #1 for your match result.

//([^/]+)/

Explanation:

//         # '//'
(          # group and capture to \1:
  [^/]+    #   any character except: '/' (1 or more times)
)          # end of \1
/          # '/'
Sign up to request clarification or add additional context in comments.

1 Comment

Quick and easy. Thank you! Appreciate the explanation too.
2

You could use the below regex which uses lookarounds.

(?<=\/\/)[^\/]+(?=\/)

Comments

2

Since the strings are always of the same format, you can simply split them on / and then retrieve the element at index 2 (the third element):

PS > $str = "//Manhattan/Project"
PS > $str.split('/')[2]
Manhattan
PS > $str = "//subject/some_other_stuff"
PS > $str.split('/')[2]
subject
PS >

1 Comment

Thank you. Didn't even know you could retrieve a particular element like that.

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.