Regular Expression help

I

Have below value in a string variable.

<APP_ID>MMM</APP_ID>user <APP_ID>MPE</APP_ID>user
:::::
<APP_ID>ITT</APP_ID>xxxx

I want to replace highlighted string(XXXX) with a variable(Var1) from pipeline. The length of XXXX varies.

Wondering How it will be done using pub.string.replace function with the help of regular expression.

Thanks.

Why don’t you just overwrite the variable “Role” which variable “VAR1” in pipeline instead of using replace function?

Hi I am new to webMethods (3 days) but am familiar with regex. Provided you can backreference with the replace function and provided there is a single role element, you can replace the following:

(<ROLE>).*(</ROLE>)

with

\1YOUR STRING\2

and you should be good to go.

Looking at the built in services guide, it seems you can use backreferencing by specifying $1…$n, ie

$1YOUR STRING$2

In your case it looks like you would need to do some concatenation of your pipeline string as well to build the replacestring to look like above.

This is probably more for reference as it would seem the first reply asks a pertinent question.

Just adding to Chris’ point, the regex setting you probably need are:


Search string = (.+)>ITT</APP_ID><ROLE>.*?</ROLE>(.+)

Replace string = $1>ITT</APP_ID><ROLE>%Var1%</ROLE>$2
          (You need to check 'Perform Variable Substitution')

The important things about the regexs are the two backreferences ($1 and $2) and the “minimal greediness” setting for the .* pattern (“.*?”). For more on Regular expression pattern greediness, have a look at [url]Perl Documentation - Perldoc Browser (search for “greedy”)

As the previous posters mention, regexs are not the best choice (though this one should be safe if document is XML). If possible, convert the string to a record structure, manipulate the record, then convert it back to string.

Why not convert it to a document and then do a mapping from VAR1 to that particular ROLE?

You may want to loop over the APP element and then do a BRANCH on the ROLE element, then have a map case where ROLE = ‘xxxx’, map VAR1 → ROLE.

Much simpler and no need to write a complicated regex

HTH,

Aditya

Why regex’s are not the best choice?

I’ll offer my two bits…

“Best” is always a subjective matter, but in general the “IS way” is to convert an incoming item to an IS document, map it to another IS document, then convert that IS document to the representation needed by the target (XML, CSV, etc.)

Manipulating XML strings directly via parsing (via regex, node queries, XSLT, etc.) is certainly doable and in some situations the advisable thing to do. But those scenarios are probably the exception cases for most IS development.

I always think Regex’s are very powerful and the data can be manipulated very fastly provided we use the correct & optimized Regex. I prefer Regex wherever possible in wM.

Hope.

I completely back Rob’s position. Regex’s should be used sparingly and commented generously. Otherwise, you risk a maintenance nightmare due to the ‘smartness’ they require. I myself had to revisit my post above where I suggest a regex to the original poster.

There are also limits to webMethods’ regex engine. For example, the PCRE “i,m,s,x” regex-modifiers (e.g., ‘i’ to ignore case, ‘m’ for multi-line match) are not available in webMethods as far as I know.

Here’s a post I made a few years ago on the unix forums - it also has a link to Rob’s excellent wmusers’ article on regex’s:
[url]regular expression [^ ]

Oh! I didn’t know about this until now. Good to know that. Thanks for your comments, Rob & Sonam.

Hope.