mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-10-17 14:00:51 +00:00
d91c97d15d
* Replace underscores with hyphens * remove temporary code * fix style check * fix collapse
95 lines
4.6 KiB
Markdown
95 lines
4.6 KiB
Markdown
---
|
||
machine_translated: true
|
||
machine_translated_rev: d734a8e46ddd7465886ba4133bff743c55190626
|
||
toc_priority: 42
|
||
toc_title: "\u6587\u5B57\u5217\u3067\u7F6E\u63DB\u3059\u308B\u5834\u5408"
|
||
---
|
||
|
||
# 文字列の検索と置換のための関数 {#functions-for-searching-and-replacing-in-strings}
|
||
|
||
## replaceOne(干し草の山,パターン,交換) {#replaceonehaystack-pattern-replacement}
|
||
|
||
が存在する場合は、その最初のオカレンスを置き換えます。 ‘pattern’ の部分文字列 ‘haystack’ と ‘replacement’ サブストリング。
|
||
以後, ‘pattern’ と ‘replacement’ 定数である必要があります。
|
||
|
||
## replaceAll(干し草の山、パターン、交換)、交換(干し草の山、パターン、交換) {#replaceallhaystack-pattern-replacement-replacehaystack-pattern-replacement}
|
||
|
||
すべての出現を置き換えます。 ‘pattern’ の部分文字列 ‘haystack’ と ‘replacement’ サブストリング。
|
||
|
||
## replaceRegexpOne(haystack,pattern,replacement) {#replaceregexponehaystack-pattern-replacement}
|
||
|
||
を使用して交換 ‘pattern’ 正規表現。 Re2正規表現。
|
||
最初のオカレンスのみを置き換えます(存在する場合)。
|
||
パターンは次のように指定できます ‘replacement’. このパタ `\0-\9`.
|
||
置換 `\0` 正規表現全体を含みます。 置換 `\1-\9` サブパターンに対応しますnumbers.To を使用 `\` テンプレート内の文字をエスケープします `\`.
|
||
また、文字列リテラルには余分なエスケープが必要です。
|
||
|
||
例1. 日付をアメリカ形式に変換する:
|
||
|
||
``` 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
|
||
```
|
||
|
||
``` 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
|
||
```
|
||
|
||
例2. 文字列を十回コピーする:
|
||
|
||
``` sql
|
||
SELECT replaceRegexpOne('Hello, World!', '.*', '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0') AS res
|
||
```
|
||
|
||
``` text
|
||
┌─res────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
|
||
│ Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World!Hello, World! │
|
||
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
|
||
```
|
||
|
||
## replaceRegexpAll(干し草の山,パターン,置換) {#replaceregexpallhaystack-pattern-replacement}
|
||
|
||
これは同じことをしますが、すべての出現を置き換えます。 例えば:
|
||
|
||
``` sql
|
||
SELECT replaceRegexpAll('Hello, World!', '.', '\\0\\0') AS res
|
||
```
|
||
|
||
``` text
|
||
┌─res────────────────────────┐
|
||
│ HHeelllloo,, WWoorrlldd!! │
|
||
└────────────────────────────┘
|
||
```
|
||
|
||
例外として、正規表現が空の部分文字列で処理された場合、置換は複数回行われません。
|
||
例えば:
|
||
|
||
``` sql
|
||
SELECT replaceRegexpAll('Hello, World!', '^', 'here: ') AS res
|
||
```
|
||
|
||
``` text
|
||
┌─res─────────────────┐
|
||
│ here: Hello, World! │
|
||
└─────────────────────┘
|
||
```
|
||
|
||
## regexpQuoteMeta(s) {#regexpquotemetas}
|
||
|
||
この関数は、文字列内のいくつかの定義済み文字の前に円記号を追加します。
|
||
定義済み文字: ‘0’, ‘\\’, ‘\|’, ‘(’, ‘)’, ‘^’, ‘$’, ‘.’, ‘\[’, ‘\]’, ‘?’, ‘\*‘,’+‘,’{‘,’:‘,’-’.
|
||
この実装はre2::re2::quotemetaとは若干異なります。 ゼロバイトを\\0の代わりに\\00としてエスケープし、必要な文字だけをエスケープします。
|
||
詳細については、リンク: [RE2](https://github.com/google/re2/blob/master/re2/re2.cc#L473)
|
||
|
||
[元の記事](https://clickhouse.tech/docs/en/query_language/functions/string_replace_functions/) <!--hide-->
|