1

I have a string, Can be any of the below cases:

  1. test1/test2/test3/test4/test5/
  2. test1/test2/test3/test4//
  3. test1/test2/test3///
  4. test1/test2////
  5. test1/////

My expected results are

  1. test1/test2/test3/test4/test5
  2. test1/test2/test3/test4

  3. test1/test2/test3

  4. test1/test2
  5. test1 How can i achieve using regex ?

Currently, i am using regexp_replace(col, "/+/", "/") it is working but leaving an extra / on the end.

2
  • 1
    Split at /, filter out empty strings from the array, join with /. Commented Oct 4, 2018 at 19:30
  • Did my answer help? Commented Jul 12, 2019 at 9:41

2 Answers 2

2

You may use

regexp_replace(col, '/+$|(/){2,}', '\\1')

See the regex demo.

Details

  • /+$ - 1 or more / at the end of the string
  • | - or
  • (/){2,} - two or more slashes, the last of which will be saved in Capturing group 1 that you will be able to refer to from the replacement pattern using the \1 placeholder.
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following regex:

/\/+$/gm

and replace with an empty string ('').

The regex will match one or more slashes in the end of the string, then will replace those with the empty string, meaning the paths will no longer end in a slash.

Comments

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.