Mike Casan Ballester
Mike Casan Ballester

Reputation: 1770

How to replace a specific character in a substring using regex?

My objective

This question is about Visual Studio Code search box.

I want to replace all the "spaces" by the character "_" inside the my substring with spaces of mystring

for example:

-> From

{
    "Show_details": mystring("my substring with spaces"),
    "Type_of_event": mystring("my substring with spaces2"),
}

-> To

{
    "Show_details": mystring("my_substring_with_spaces"),
    "Type_of_event": mystring("my_substring_with_spaces2"),
}

What I tried

I only managed to get the `my_substring_with_spaces`. But I do not understand how to proceed further.
Search: mystring\("([a-z]*?( )[a-z]*?)"\)

Upvotes: 2

Views: 193

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627082

You can use

(?<=mystring\("[^"]*)\s+(?=[^"]*"\))

Details

  • (?<=mystring\("[^"]*) - immediately to the left, there must be mystring(" and then 0 or more chars other than "
  • \s+ - one or more whitespaces (remove + if two consecutive spaces must be converted to two consecutive _s)
  • (?=[^"]*"\)) - immediately to the right, there must be 0 or more chars other than " and then ").

See the screenshot:

enter image description here

Upvotes: 2

Related Questions