regarding regexp

thota198
thota198 used Ask the Experts™
on
wht is the below code tell can anyone explain

 REGEXP_REPLACE (io_message_body,
                         '(^|\W)' || LOWER ('<BODY>') || '(\W|$)',
                         '\1' || LOWER ('<BODY>' || v_message_body) || '\2',
                         1,
                         0,
                         'i'
                        );
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
That is a bit confusing on first glance, as || is the Oracle string concatenation operator, and | is "OR" in regexp. We can simplify the first part:

 REGEXP_REPLACE (io_message_body,
                         '(^|\W)<body>(\W|$)',
                         '\1' || LOWER ('<BODY>' || v_message_body) || '\2',
                         1, 0, 'i'
                        );

So, the search string is a start-of-line (^) or (|) non-word character (\W) [not: 0-9, A-Z, a-z, underscore], folllowed by <body>, and a non-word-character or end-of-line ($).
This is then replaced with: first group found (\1) [a group is a regexp in parens], that is the non-word character or start-of-line, then body tag is extended by some text, followed by the second group.

Or short: replace "<body>" with "<body>SomeMessageBody" by retaining a optional non-word character prefixing and/or suffixing the "<body>" tag, but that tag needs to stand on an own line.

I hope that was less confusing, but regexp aren't easy to understand ... A reference can be found at http://en.wikipedia.org/wiki/Regular_expression  (and many other locations).
Top Expert 2011
Commented:
- to add, REGEXP_REPLACE is a function to search a string for a regular expression pattern where each matching string is replaced with the string specified. the syntax is regexp_replace(source, pattern, replacement [, flags ]) where in your expression above:
source: io_message_body
pattern: '(^|\W)<body>(\W|$)',
replacement: '\1' || LOWER ('<BODY>' || v_message_body) || '\2',
flags: position=1, occurance=0, matchparameter: i

- i'm suggesting you to start with the Documentation on regular expression to have better understanding of the syntax:
REGEXP_REPLACE:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions130.htm

- regular expression operators: 
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/ap_posix001.htm#BABJDBHB

Author

Commented:
thanks a lot

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial