正则表达式中的单引号和反引号
我正在尝试匹配字符串中的单引号,如果存在,我想使用 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.
我遇到的问题是我不理解 ABAP 正则表达式中反引号字符的行为。并且无法在网上找到解释其工作原理的资源。我使用它得到的结果非常奇怪,具体取决于匹配项上方的 TEXT 字符串是否有效。
在 PERL 中,您可以执行诸如在字符串中搜索字符串变量之类的操作,例如/:
my $tofind = "'"; //a single quote
my $text = "this is a string containing a ' single quote";
if($text=~m/$tofind/){
print "found";
}
此方法可以在 ABAP 中使用吗?或者有人可以解释如何在 ABAP 中使用反引号吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,您不需要反引号,但是我认为您正在寻找的是转义字符,以表明 ' 是字符串/正则表达式的一部分,
请注意定义的字符串和正则表达式中的额外 ' 。
编辑:为了回应您对文档的评论,如果您将光标定位在单词 regex 上按 F1(帮助),您应该能够访问正则表达式的 SAP 帮助。看起来确实大部分语法都记录在那里。 (或者,只需按 F1 并搜索正则表达式)。
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
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).
顺便说一句,有更简单的方法可以找到单个字符。
as a side note, there are easier ways to find a single character.
在 ABAP 中,反引号不特定于正则表达式,它们用于定义键入
字符串
(与类型“C”相对)。您唯一的 ABAP 示例代码运行良好。
不幸的是,您的问题不清楚,因为您没有提供无法正常工作的示例,因此无法准确回答。
有关代码正则表达式的信息:
\
(反斜杠) 是一种“unescape”字符:当您想要搜索通常具有特殊含义的字符时使用它,例如这些字符:.()|{}[]
(等)作为单个quote'
不是特殊字符,不需要指明\
%
(百分号)在ABAP正则表达式中没有特殊含义,也不在文本中。我猜您使用%27
来对应 Unicode 代码点 U+0027 的单引号(又名撇号),但它由 ABAP 按字面解释,因此它仅对应于三个单独字符的序列。In ABAP, backquotes are not specific to regular expressions, they are used to define character literals typed
string
(opposed to type "C").Your only ABAP example code works fine.
Unfortunately, your question is unclear as you don't provide an example of what is not working correctly, so it's impossible to answer precisely.
For information, about the regular expression of your code:
\
(backslash) is a kind of "unescape" character: it's to be used when you want to search a character that has normally a special meaning, like those characters:.()|{}[]
(etc.) As the single quote'
is not a special character, you don't need to indicate\
%
(percentage sign) has no special meaning in ABAP regular expressions, nor in text literals. I guess you used%27
to correspond to the Unicode code point U+0027 of the single quote (AKA apostrophe), but instead it's interpreted literally by ABAP so it will only correspond to the sequence of the three individual characters.