pub.string.replace and regular expression

Greetings,

I am trying to use the pub.string.replace service to modify only the first
occurrence of a string but am having no luck figuring out the regular expression (searchString).

I would like to invoke the service on this string (inString):
‘String1 String2 String1 String2’

and want it to look like:
‘String1 String2Replaced String1 String2’

Note that I only want to change the first occurrence of ‘String2’ with ‘String2Replaced’ (replaceString).

Any help would be greatly appreciated.

Thanks.

using pub.string.substring built-in service to extract the value from the input string and map the result into pub.string:replace (search string) and enter the replace string value…its simple.

HTH

Thanks but I only want to replace the first occurrence.

Still looking for the regular expression to accomplish the task…

Maybe my example was confusing…

I would like to invoke the service on this string (inString):
‘XXXXX YYYYY XXXXX YYYYY’

and want it to look like:
‘XXXXX ZZZZZ XXXXX YYYYY’

Note that I only want to change the first occurrence of ‘YYYYY’ with ‘ZZZZZ’ (replaceString).

Using negative lookbehind:

(?<!.*String2)String2

would do the trick. Alas, this doesn’t work with pub.string:replace as it is presumably using a regex compiler which doesn’t support lookbehind.

A couple of options to consider:

  • You could roll your own pub.string:replace equivalent and use the java.util.regex classes that support lookbehind.

  • Use PSUtilities.regex:replaceFirst

That’ll do it. Thanks a lot!