diff --git a/docs/reference/query-languages/esql/_snippets/functions/examples/replace.md b/docs/reference/query-languages/esql/_snippets/functions/examples/replace.md index e2a2dffa4e8bc..ccc32441b3223 100644 --- a/docs/reference/query-languages/esql/_snippets/functions/examples/replace.md +++ b/docs/reference/query-languages/esql/_snippets/functions/examples/replace.md @@ -1,17 +1,27 @@ % This is generated by ESQL's AbstractFunctionTestCase. Do not edit it. See ../README.md for how to regenerate it. -**Example** +**Examples** This example replaces any occurrence of the word "World" with the word "Universe": ```esql ROW str = "Hello World" | EVAL str = REPLACE(str, "World", "Universe") -| KEEP str ``` | str:keyword | | --- | | Hello Universe | +This example removes all spaces: + +```esql +ROW str = "Hello World" +| EVAL str = REPLACE(str, "\\\\s+", "") +``` + +| str:keyword | +| --- | +| HelloWorld | + diff --git a/docs/reference/query-languages/esql/kibana/definition/functions/replace.json b/docs/reference/query-languages/esql/kibana/definition/functions/replace.json index 1bed5c68284f1..afc95a26894ea 100644 --- a/docs/reference/query-languages/esql/kibana/definition/functions/replace.json +++ b/docs/reference/query-languages/esql/kibana/definition/functions/replace.json @@ -198,7 +198,8 @@ } ], "examples" : [ - "ROW str = \"Hello World\"\n| EVAL str = REPLACE(str, \"World\", \"Universe\")\n| KEEP str" + "ROW str = \"Hello World\"\n| EVAL str = REPLACE(str, \"World\", \"Universe\")", + "ROW str = \"Hello World\"\n| EVAL str = REPLACE(str, \"\\\\\\\\s+\", \"\")" ], "preview" : false, "snapshot_only" : false diff --git a/docs/reference/query-languages/esql/kibana/docs/functions/replace.md b/docs/reference/query-languages/esql/kibana/docs/functions/replace.md index 23eb152bb0789..cc42407d73418 100644 --- a/docs/reference/query-languages/esql/kibana/docs/functions/replace.md +++ b/docs/reference/query-languages/esql/kibana/docs/functions/replace.md @@ -7,5 +7,4 @@ with the replacement string `newStr`. ```esql ROW str = "Hello World" | EVAL str = REPLACE(str, "World", "Universe") -| KEEP str ``` diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/docs.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/docs.csv-spec index 1d2fade118e35..d4d42616f7509 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/docs.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/docs.csv-spec @@ -335,20 +335,6 @@ date_string:keyword | date:date // end::dateParse-result[] ; -docsReplace -//tag::replaceString[] -ROW str = "Hello World" -| EVAL str = REPLACE(str, "World", "Universe") -| KEEP str -// end::replaceString[] -; - -//tag::replaceString-result[] -str:keyword -Hello Universe -// end::replaceString-result[] -; - docsCase // tag::case[] FROM employees diff --git a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/string.csv-spec b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/string.csv-spec index e681d09fb9aa6..49d3469aac41d 100644 --- a/x-pack/plugin/esql/qa/testFixtures/src/main/resources/string.csv-spec +++ b/x-pack/plugin/esql/qa/testFixtures/src/main/resources/string.csv-spec @@ -1223,6 +1223,33 @@ first_name:keyword | result:keyword Alejandro | null ; +docsReplaceText +//tag::replaceString[] +ROW str = "Hello World" +| EVAL str = REPLACE(str, "World", "Universe") +// end::replaceString[] +; + +//tag::replaceString-result[] +str:keyword +Hello Universe +// end::replaceString-result[] +; + +docsReplaceRegex +//tag::replaceRegex[] +ROW str = "Hello World" +| EVAL str = REPLACE(str, "\\s+", "") +// end::replaceRegex[] +; + +//tag::replaceRegex-result[] +str:keyword +HelloWorld +// end::replaceRegex-result[] +; + + left // tag::left[] FROM employees diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/Replace.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/Replace.java index 5da7902d4091d..d318f96187d9f 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/Replace.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/Replace.java @@ -50,11 +50,13 @@ public class Replace extends EsqlScalarFunction { description = """ The function substitutes in the string `str` any match of the regular expression `regex` with the replacement string `newStr`.""", - examples = @Example( - file = "docs", - tag = "replaceString", - description = "This example replaces any occurrence of the word \"World\" with the word \"Universe\":" - ) + examples = { + @Example( + file = "string", + tag = "replaceString", + description = "This example replaces any occurrence of the word \"World\" with the word \"Universe\":" + ), + @Example(file = "string", tag = "replaceRegex", description = "This example removes all spaces:") } ) public Replace( Source source,