I'm trying to match a single quote in a string, if it exists I want to display this using WRITE
.
data text type string value `this is a string containing a ' single quote`.
find regex `(\'|%27)` in text.
if sy-subrc = 0.
write 'found'.
endif.
The problem I'm having is that I don't understand the behaviour of the backquote character in ABAP regex's. And cannot find a resource online with an explanation of how it works. The results I'm getting using it are quite odd, depending on the TEXT string above the match either works or fails.
In PERL you can do things like search for a string variable within a string, eg/:
my $tofind = "'"; //a single quote
my $text = "this is a string containing a ' single quote";
if($text=~m/$tofind/){
print "found";
}
Can this method be used in ABAP, or could someone explain how to use backquotes in ABAP?
You don't need the backquote in this case, however what I think you are looking for is the escape character to show that the ' is part of the string/regex
data text type string value `this is a string containing a '' single quote`.
find regex `(''|%27)` in text.
if sy-subrc = 0.
write 'found'.
endif.
Note the extra ' in both the defined string as well as the regex.
Edit: In response to your comment regarding the documentation, if you do an F1 (Help) with your cursor positioned on the word regex, you should be able to access the SAP help for regular expressions. It did look like most of the syntax were documented there. (Alternatevily, just press F1 and search for regex).