Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find string between slashes

Tags:

regex

I am trying to write a regex (with very little luck) to capture a string that occurs between two slashes in a specific location within a larger string.

An example source string would be: There will be some text description here followed by the path: /XYZ/String-needed/ABC/ETC

The string-needed will always be between the second and third / character.

The context of regex for this is within another tool, but it models mostly a java context of regex usage. I know that the / will need to be escaped \/ but I cannot get the code to find the string between the second and third instance.

like image 967
Buckwheattb Avatar asked Feb 10 '23 03:02

Buckwheattb


1 Answers

You can use following regex :

"^\/[^/]+\/([^/]+)\/"

See demo https://regex101.com/r/jR9rV5/1

[^/] is a negated character class which match any string except /.

like image 81
Mazdak Avatar answered Feb 23 '23 17:02

Mazdak