0

For example, I have two strings

  1. "abcdef"
  2. "abcdefghijk"

I want to replace the first one, but not the second one which begins with the same string. However, the first one can be followed by any ponctuation such like , ; ? . and so on. So, I have to replace abcdef only, if there is no other character A-Z, a-z, after the string.

I probably have to use preg_replace, but I don't see how to write my regex string to exclude the strings followed by any character.

I tried of course to replace the string without to take care of the characters following this string, and it is running of course. str_replace is enough for that, but it is not enough in my case.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
ray61
  • 11
  • 1
  • 1
    Perhaps https://stackoverflow.com/questions/6494915/regex-pattern-to-match-the-end-of-a-string can help. – Nigel Ren Aug 22 '23 at 07:27
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 22 '23 at 09:18
  • 1
    Could you provide more example data? both of subject and pattern. – Allen Chak Aug 22 '23 at 09:30

1 Answers1

0

Use a negative lookahead that makes sure we haven't letter after the match.

\babcdef(?![a-zA-Z])

Demo & Explanation

Toto
  • 89,455
  • 62
  • 89
  • 125