替换功能中的特殊字符 [英] special characters in replace function

查看:143
本文介绍了替换功能中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GREL替换函数需要3个字符串,或一个字符串,一个正则表达式和一个字符串. 在用于替换的第三个字符串中,某些字符具有特殊的行为:\,\,\ t,\ n,\',\"以及其他一些组合. \不执行任何操作,否则发生错误 \被解释为\ \ t被解释为制表符 \ n被解释为换行 \"被解释为 \'被解释为'

GREL replace function expects 3 strings, or a string, a regex and a string. In the 3rd string used for replacement, some characters have a special behavior : \, \, \t, \n, \', \" and maybe some other combinations. \ does nothing, or an error \ is interpreted as \ \t is interpreted as a tab character \n is interpreted as a new line \" is interpreted as " \' is interpreted as '

例如: "abab" .replace('b',"\")->在偏移量19处解析错误:缺少数字,字符串,标识符,正则表达式或带括号的表达式"

Ex : "abab".replace('b',"\") -> "Parsing error at offset 19: Missing number, string, identifier, regex, or parenthesized expression"

"abab" .replace('b',"\ t")-> a

"abab".replace ('b',"\t") -> a a

我想它与Java有关... 还有其他特殊组合吗? 它记录在Wiki上的某个地方吗?

I suppose it has something to do with Java... Is there other special combinations? Is it documented somewhere on the wiki?

推荐答案

在字符串中,反斜杠(\)具有特殊含义.它基本上说,不应以通常的意义来考虑以下字符.这就是为什么字符串"t"只是字母t,而"\ t"表示tab的原因.

In a string, the backslash (\) has a special meaning. It basically says that the following character should not be considered in its usual sense. This is why the string "t" is just the letter t, but "\t" means a tab.

此转义字符还用于在字符串中包含引号.例如,字符串"L'alouette"将引发错误,因为它包含的引号与字符串周围的引号类型相同.可以通过转义内部的':'L \'alouette'(或使用双引号:"l'alouette")来避免此问题

This escape character is also used to include quotation marks in a string. The string 'L'alouette', for example, will trigger an error, since it contains a quotation mark of the same type as the one surrounding the string. The problem can be avoided by escaping the inner ' : 'L\'alouette' (or by using double quotes: "l'alouette")

在您的示例中,OpenRefine理解您要转义第二个引号("\"),并认为您的字符串未完成.在这种情况下,正确的语法应该是转义\本身:"abab".replace('b', "\\")

In your example, OpenRefine understands that you want to escape the second quotation mark ("\") and considers that your string is not finished. The correct syntax, in this case, would be to escape the \ itself : "abab".replace('b', "\\")

特殊字符列表

| Special characters | Display               |
|--------------------|-----------------------|
| \'                 | Single quotation mark |
| \"                 | Double quotation mark |
| \\                 | Backslash             |
| \t                 | Tab                   |
| \b                 | Backspace             |
| \r                 | Carriage return       |
| \f                 | Formfeed              |
| \n                 | Newline               |

这篇关于替换功能中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆