3

my string is like this sfdfdsfdsfstart112matlab2336endgfdgdfgkknfkgstart558899enddfdsfd

how can we replace part of a string such a way that the result will be

sfdfdsfdsfgfdgdfgkknfkgdfdsfd

i.e bolded content need to be removed.

Sai Mukesh
  • 401
  • 4
  • 11

3 Answers3

20

You need to use non-greedy matching:

start.*?end
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
5

Use replacement function with this regex /start.+?end/g which will match the bold parts of your string. The g part of the regex means globally, and might need to be implemented differently depending on the language you use.

The key here is to use ? which turns on un-greedy matching. That means the match consumes the minimum amount of characters rather than the maximum, so will match from the start to the next rather than the last end

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
0
start[1-9]+end

if you need to have numbers between words

bumbu
  • 1,297
  • 11
  • 29