0

Is it a way using Regex to extract the string between a 2 occurrence of a character then replace it with another character. I know that's a bit tricky but i believe it's possible using Regex .. I just tried many ways but seem not working with me.

What i want to achieve is Extracting from this string:

 TestNumQ_RY_5100001_OS35QWE_20190702-2.ZIP

The String between the First and Third _:

RY_5100001

Then Replace this _ with / so the end result will be:

RY/5100001

What i can reach until now is using this:

(_(.*?)_)([^_]*)

Which retrieve only the below string:

_RY_5100001

But i need it to be:

RY_5100001
Marzouk
  • 2,278
  • 3
  • 20
  • 46
  • 1
    Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/a/2759417/3832970) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Sep 13 '19 at 14:21
  • Honestly and after checking most of the answers i can't find similar scenario. I've updated the question – Marzouk Sep 13 '19 at 15:39
  • 1
    The best you can do is use a replacing approach, see http://regex101.com/r/2nRHDM/1. You can extract and replace within one regex match operation. – Wiktor Stribiżew Sep 13 '19 at 15:46
  • 1
    If you jusf need `RY_5100001` use a mere `_([^_]*_[^_]*)_` and grab Groul 1 value. There may be other solutions but they all depend on the environment where you are using the regex. – Wiktor Stribiżew Sep 13 '19 at 16:09
  • 1
    return string.match(/[^_]*_([^_]*_[^_]*)_.*/)[1];// works in chrome interpreter – Chris Belyeu Sep 13 '19 at 22:33

0 Answers0