文本函数
使用 Levenshtein 距离比较字符串
使用 StringUtils.distance(text1, text2)
方法(Levenshtein)比较给定的 STRING
值。
RETURN apoc.text.distance("Levenshtein", "Levenstein") // 1
语音比较函数
语音文本 (Soundex) 函数允许您计算给定字符串的 Soundex 编码。还有一个过程用于比较两个字符串在 Soundex 算法下听起来的相似程度。所有 Soundex 过程默认假定所使用的语言是美式英语。
|
返回 STRING 中所有词语的美式英语语音 Soundex 编码。 |
|
返回给定 |
|
去除给定 |
|
比较两个给定 |
|
返回两个给定 |
// will return 'H436'
RETURN apoc.text.phonetic('Hello, dear User!')
// will return '4' (very similar)
RETURN apoc.text.phoneticDelta('Hello Mr Rabbit', 'Hello Mr Ribbit')
文本格式化
使用给定参数和可选参数语言格式化给定 STRING
。
RETURN apoc.text.format('ab%s %d %.1f %s%n',['cd', 42, 3.14, true]) AS value // abcd 42 3.1 true
RETURN apoc.text.format('ab%s %d %.1f %s%n',['cd', 42, 3.14, true],'it') AS value // abcd 42 3,1 true
字符串搜索
indexOf
函数提供给定 lookup
字符串在 text
中的首次出现位置,如果未找到则返回 -1。它还可以选择性地接受 from
(包含)和 to
(不包含)参数。
RETURN apoc.text.indexOf('Hello World!', 'World') // 6
indexesOf
函数提供给定 lookup
字符串在 text
中的所有出现位置,如果未找到则返回空列表。它还可以选择性地接受 from
(包含)和 to
(不包含)参数。
RETURN apoc.text.indexesOf('Hello World!', 'o',2,9) // [4,7]
获取从索引匹配开始的子字符串
World!
WITH 'Hello World!' as text, length(text) as len
WITH text, len, apoc.text.indexOf(text, 'World',3) as index
RETURN substring(text, case index when -1 then len-1 else index end, len);
正则表达式
RETURN apoc.text.replace('Hello World!', '[^a-zA-Z]', '')
RETURN apoc.text.regexGroups('abc <link xxx1>yyy1</link> def <link xxx2>yyy2</link>','<link (\\w+)>(\\w+)</link>') AS result
// [["<link xxx1>yyy1</link>", "xxx1", "yyy1"], ["<link xxx2>yyy2</link>", "xxx2", "yyy2"]]
RETURN apoc.text.regexGroupsByName(
'abc <link xxx1>yyy1</link> def <link xxx2>yyy2</link>',
'<link (?<firstPart>\\w+)>(?<secondPart>\\w+)</link>'
) AS output;
// [{ "group": "<link xxx1>yyy1</link>", "matches" : {"firstPart": "xxx1", "secondPart": "yyy1"}}, {"group": <link xxx2>yyy2</link>", "matches" : { "firstPart": "xxx2", "secondPart": "yyy2"}}]
分割与连接
RETURN apoc.text.split('Hello World', ' +')
RETURN apoc.text.join(['Hello', 'World'], ' ')
数据清洗
RETURN apoc.text.clean('Hello World!')
true
RETURN apoc.text.compareCleaned('Hello World!', '_hello-world_')
UNWIND ['Hello World!', 'hello worlds'] as text
RETURN apoc.text.filterCleanMatches(text, 'hello_world') as text
清洗功能对于清理格式不一致的微脏文本数据,以便进行非精确比较非常有用。
清洗会剥离字符串中所有非字母数字字符(包括空格),并将其转换为小写。
大小写转换函数
capitalize
将单词的首字母大写RETURN apoc.text.capitalize("neo4j") // "Neo4j"
capitalizeAll
将文本中每个单词的首字母大写RETURN apoc.text.capitalizeAll("graph database") // "Graph Database"
decapitalize
将字符串的首字母小写RETURN apoc.text.decapitalize("Graph Database") // "graph Database"
decapitalizeAll
将所有单词的首字母小写RETURN apoc.text.decapitalizeAll("Graph Databases") // "graph databases"
swapCase
切换字符串的大小写RETURN apoc.text.swapCase("Neo4j") // nEO4J
camelCase
将字符串转换为小驼峰命名法RETURN apoc.text.camelCase("FOO_BAR"); // "fooBar"
RETURN apoc.text.camelCase("Foo bar"); // "fooBar"
RETURN apoc.text.camelCase("Foo22 bar"); // "foo22Bar"
RETURN apoc.text.camelCase("foo-bar"); // "fooBar"
RETURN apoc.text.camelCase("Foobar"); // "foobar"
RETURN apoc.text.camelCase("Foo$$Bar"); // "fooBar"
upperCamelCase
将字符串转换为大驼峰命名法RETURN apoc.text.upperCamelCase("FOO_BAR"); // "FooBar"
RETURN apoc.text.upperCamelCase("Foo bar"); // "FooBar"
RETURN apoc.text.upperCamelCase("Foo22 bar"); // "Foo22Bar"
RETURN apoc.text.upperCamelCase("foo-bar"); // "FooBar"
RETURN apoc.text.upperCamelCase("Foobar"); // "Foobar"
RETURN apoc.text.upperCamelCase("Foo$$Bar"); // "FooBar"
snakeCase
将字符串转换为蛇形命名法RETURN apoc.text.snakeCase("test Snake Case"); // "test-snake-case"
RETURN apoc.text.snakeCase("FOO_BAR"); // "foo-bar"
RETURN apoc.text.snakeCase("Foo bar"); // "foo-bar"
RETURN apoc.text.snakeCase("fooBar"); // "foo-bar"
RETURN apoc.text.snakeCase("foo-bar"); // "foo-bar"
RETURN apoc.text.snakeCase("Foo bar"); // "foo-bar"
RETURN apoc.text.snakeCase("Foo bar"); // "foo-bar"
toUpperCase
将字符串转换为大写RETURN apoc.text.toUpperCase("test upper case"); // "TEST_UPPER_CASE"
RETURN apoc.text.toUpperCase("FooBar"); // "FOO_BAR"
RETURN apoc.text.toUpperCase("fooBar"); // "FOO_BAR"
RETURN apoc.text.toUpperCase("foo-bar"); // "FOO_BAR"
RETURN apoc.text.toUpperCase("foo--bar"); // "FOO_BAR"
RETURN apoc.text.toUpperCase("foo$$bar"); // "FOO_BAR"
RETURN apoc.text.toUpperCase("foo 22 bar"); // "FOO_22_BAR"
Base64 解码与编码
以 base64 或 base64Url 编码或解码字符串
RETURN apoc.text.base64Encode("neo4j") // bmVvNGo=
RETURN apoc.text.base64Decode("bmVvNGo=") // neo4j
RETURN apoc.text.base64UrlEncode("https://neo4j.ac.cn/?test=test") // aHR0cDovL25lbzRqLmNvbS8_dGVzdD10ZXN0
RETURN apoc.text.base64UrlDecode("aHR0cDovL25lbzRqLmNvbS8_dGVzdD10ZXN0") // https://neo4j.ac.cn/?test=test