nbonbon
nbonbon

Reputation: 1757

Replacing text in between certain symbols

I am given a string like this: CSF@asomedatahere@iiwin@hnotwhatIwant

And I want to replace the string that is present BETWEEN @i and @h (h could be any character) . This is what I have so far and I feel that I am close, however, there may not always be a @CHAR after this @idata pattern.

 (?<=@i)(.*)(?=@.*)

I would like it to work for that optionally not being there. As it can be seen in the link below it works for the first case not the second. I tried adding a '?' at the end to make the last part optional but that makes it not work for the first case.

Here is a link that will show you actively what is not working: http://fiddle.re/vtvmc

Upvotes: 0

Views: 66

Answers (1)

Thomas
Thomas

Reputation: 88707

You need to expand the look-ahead to use the end of the input as well:

(?<=@i)(.*?)(?=@.*|$)

This would match

  • iwin@hnotwhatIwant in CSF@asomedatahere@iiwin@hnotwhatIwant
  • iwin@h in CSF@asomedatahere@iiwin@h
  • iwin in CSF@asomedatahere@iiwin.

Upvotes: 3

Related Questions