/scripts/ahub.form.attachments.js
-1

How do I find a value with in a substring REGEX

Apr 02, 2017 at 02:17 PM

107

avatar image

HI,

I am trying to find a character with in a substring. For example I have a | delimited file and want to remove all | values between " ".

|2017-03-29|"DEC | User X"|Y|2017-03-29|"DEC | User Z"|Y|

I am trying to located the | in the substring |"DEC | User X"| and any other occurrence it appears.

Thanks

10 |10000 characters needed characters left characters exceeded

Probably it is better to ask this question on a dedicated Regex forum. Regex is a powerful, but sometimes quite difficult to use. I wouldn't expect ABAP developers to be expert with it.

2
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Horst Keller
Apr 02, 2017 at 02:21 PM
6

Read the documentation and play with DEMO_REGEX.

https://help.sap.com/http.svc/rc/abapdocu_751_index_htm/7.51/en-US/index.htm?file=abenregex_syntax_specials.htm

| is a special character that must be escaped by \.

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi,

Thanks for the help.. I ended up doing the following

"[^"\\]*(?:\\.[^"\\]*)*"

|2017-03-29|"DEC | User X"|Y|2017-03-29|"DEC | User Z"|Y|

which gives me

"DEC | User X" & "DEC | User Z"

Regards

Petros

0
Petros Yannoutsos
In fact, it seems that you don't want to locate the | characters,
but only parse what's inside 2 double quotes, with the \ character being
an escape character.

... REGEX 
  '"'           " Double quote
& '[^"\\]*'     " 0 character or list of any characters up to " or \
& '(?:'         " start of non-capturing group
&   '\\.'         " \ followed by any character
&   '[^"\\]*'     " list of any characters up to " or \
&   ')'           " end of group
& '*'           " Optional group or repeat the group 1 or more times
2
Sandra Rossi Apr 03, 2017 at 08:08 AM
3

I'm surprised by the question: don't you want to keep the | inside "..." fields and remove the real | separators?

If I'm right, I think you should first split at fields in a first regular expression: ([|]|"[^"]+"|[^|]+), which would produce 3 types of elements, the separators (|), the "..." fields, and the other fields. Then delete the separators, and then loop at all "..." fields to remove quotes (and so, also keep the optional | if it's there).

Share
10 |10000 characters needed characters left characters exceeded
Skip to Content