ClickHouse/docs/en/sql_reference/functions/string_replace_functions.md

93 lines
4.2 KiB
Markdown
Raw Normal View History

2020-04-03 13:23:32 +00:00
---
toc_priority: 42
toc_title: For Replacing in Strings
---
2020-03-20 10:10:48 +00:00
# Functions for searching and replacing in strings {#functions-for-searching-and-replacing-in-strings}
2020-03-20 10:10:48 +00:00
## replaceOne(haystack, pattern, replacement) {#replaceonehaystack-pattern-replacement}
2020-03-20 10:10:48 +00:00
Replaces the first occurrence, if it exists, of the pattern substring in haystack with the replacement substring.
Hereafter, pattern and replacement must be constants.
2020-03-20 10:10:48 +00:00
## replaceAll(haystack, pattern, replacement), replace(haystack, pattern, replacement) {#replaceallhaystack-pattern-replacement-replacehaystack-pattern-replacement}
2020-03-20 10:10:48 +00:00
Replaces all occurrences of the pattern substring in haystack with the replacement substring.
2020-03-20 10:10:48 +00:00
## replaceRegexpOne(haystack, pattern, replacement) {#replaceregexponehaystack-pattern-replacement}
2020-03-20 10:10:48 +00:00
Replacement using the pattern regular expression. A re2 regular expression.
Replaces only the first occurrence, if it exists.
2020-03-20 10:10:48 +00:00
A pattern can be specified as replacement. This pattern can include substitutions `\0-\9`.
The substitution `\0` includes the entire regular expression. Substitutions `\1-\9` correspond to the subpattern numbers.To use the `\` character in a template, escape it using `\`.
Also keep in mind that a string literal requires an extra escape.
Example 1. Converting the date to American format:
2020-03-20 10:10:48 +00:00
``` sql
SELECT DISTINCT
EventDate,
replaceRegexpOne(toString(EventDate), '(\\d{4})-(\\d{2})-(\\d{2})', '\\2/\\3/\\1') AS res
FROM test.hits
LIMIT 7
FORMAT TabSeparated
```
2020-03-20 10:10:48 +00:00
``` text
2014-03-17 03/17/2014
2014-03-18 03/18/2014
2014-03-19 03/19/2014
2014-03-20 03/20/2014
2014-03-21 03/21/2014
2014-03-22 03/22/2014
2014-03-23 03/23/2014
```
Example 2. Copying a string ten times:
2020-03-20 10:10:48 +00:00
``` sql
SELECT replaceRegexpOne('Hello, World!', '.*', '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0') AS res
```
2020-03-20 10:10:48 +00:00
``` text
┌─res────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World! │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
2020-03-20 10:10:48 +00:00
## replaceRegexpAll(haystack, pattern, replacement) {#replaceregexpallhaystack-pattern-replacement}
This does the same thing, but replaces all the occurrences. Example:
2020-03-20 10:10:48 +00:00
``` sql
SELECT replaceRegexpAll('Hello, World!', '.', '\\0\\0') AS res
```
2020-03-20 10:10:48 +00:00
``` text
┌─res────────────────────────┐
│ HHeelllloo,, WWoorrlldd!! │
└────────────────────────────┘
```
As an exception, if a regular expression worked on an empty substring, the replacement is not made more than once.
Example:
2020-03-20 10:10:48 +00:00
``` sql
SELECT replaceRegexpAll('Hello, World!', '^', 'here: ') AS res
```
2020-03-20 10:10:48 +00:00
``` text
┌─res─────────────────┐
│ here: Hello, World! │
└─────────────────────┘
```
2018-04-23 06:20:21 +00:00
2020-03-20 10:10:48 +00:00
## regexpQuoteMeta(s) {#regexpquotemetas}
The function adds a backslash before some predefined characters in the string.
2020-03-20 10:10:48 +00:00
Predefined characters: 0, \\, \|, (, ), ^, $, ., \[, \], ?, \*,+,{,:,-.
This implementation slightly differs from re2::RE2::QuoteMeta. It escapes zero byte as \\0 instead of 00 and it escapes only required characters.
For more information, see the link: [RE2](https://github.com/google/re2/blob/master/re2/re2.cc#L473)
2020-01-30 10:34:55 +00:00
[Original article](https://clickhouse.tech/docs/en/query_language/functions/string_replace_functions/) <!--hide-->