| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_string_slice |
| 5 |
|
| 6 |
``string_slice`` |
| 7 |
================ |
| 8 |
|
| 9 |
.. versionadded:: 11.0.3 |
| 10 |
|
| 11 |
Summary |
| 12 |
------- |
| 13 |
|
| 14 |
``string_slice`` extracts a substring of a string. You can use two different extraction methods depending on the arguments. |
| 15 |
|
| 16 |
* Extraction by position |
| 17 |
* Extraction by regular expression |
| 18 |
|
| 19 |
Groonga uses the same regular expression syntax in Ruby. |
| 20 |
|
| 21 |
To enable this function, register ``functions/string`` plugin by following the command:: |
| 22 |
|
| 23 |
plugin_register functions/string |
| 24 |
|
| 25 |
Syntax |
| 26 |
------ |
| 27 |
|
| 28 |
``string_slice`` requires two to four parameters. The required parameters are depending on the extraction method. |
| 29 |
|
| 30 |
Extraction by position |
| 31 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 32 |
|
| 33 |
:: |
| 34 |
|
| 35 |
string_slice(target, nth[, options]) |
| 36 |
string_slice(target, nth, length[, options]) |
| 37 |
|
| 38 |
``options`` uses the following format. All of key-value pairs are optional:: |
| 39 |
|
| 40 |
{ |
| 41 |
"default_value": default_value |
| 42 |
} |
| 43 |
|
| 44 |
Extraction by regular expression |
| 45 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 46 |
|
| 47 |
:: |
| 48 |
|
| 49 |
string_slice(target, regexp, nth[, options]) |
| 50 |
string_slice(target, regexp, name[, options]) |
| 51 |
|
| 52 |
``options`` uses the following format. All of key-value pairs are optional:: |
| 53 |
|
| 54 |
{ |
| 55 |
"default_value": default_value |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
Usage |
| 60 |
----- |
| 61 |
|
| 62 |
Here are a schema definition and sample data to show usage. |
| 63 |
|
| 64 |
Sample schema: |
| 65 |
|
| 66 |
.. groonga-command |
| 67 |
.. include:: ../../example/reference/functions/string_slice/usage_setup_schema.log |
| 68 |
.. plugin_register functions/string |
| 69 |
.. table_create Memos TABLE_HASH_KEY ShortText |
| 70 |
|
| 71 |
|
| 72 |
Sample data: |
| 73 |
|
| 74 |
.. groonga-command |
| 75 |
.. include:: ../../example/reference/functions/string_slice/usage_setup_data.log |
| 76 |
.. load --table Memos |
| 77 |
.. [ |
| 78 |
.. {"_key": "Groonga"} |
| 79 |
.. ] |
| 80 |
|
| 81 |
Here is a simple example for the extraction by position. |
| 82 |
|
| 83 |
.. groonga-command |
| 84 |
.. include:: ../../example/reference/functions/string_slice/usage_number.log |
| 85 |
.. select Memos --output_columns '_key, string_slice(_key, 2, 3)' |
| 86 |
|
| 87 |
Here are simple examples for the extraction by regular expression. |
| 88 |
|
| 89 |
In the following example, extracting by specifying the group number of the capturing group: ``(subexp)``. |
| 90 |
|
| 91 |
.. groonga-command |
| 92 |
.. include:: ../../example/reference/functions/string_slice/usage_regexp_number.log |
| 93 |
.. select Memos --output_columns '_key, string_slice(_key, "(Gro+)(.*)", 2)' |
| 94 |
|
| 95 |
In the following example, extracting by specifying the name of the named capturing group: ``(?<name>subexp)``. |
| 96 |
|
| 97 |
.. groonga-command |
| 98 |
.. include:: ../../example/reference/functions/string_slice/usage_regexp_name.log |
| 99 |
.. select Memos --output_columns '_key, string_slice(_key, "(Gr)(?<Name1>o*)(?<Name2>.*)", "Name1")' |
| 100 |
|
| 101 |
In the following example, specifying the default value. |
| 102 |
|
| 103 |
.. groonga-command |
| 104 |
.. include:: ../../example/reference/functions/string_slice/usage_regexp_default.log |
| 105 |
.. select Memos --output_columns '_key, string_slice(_key, "mismatch", 2, { "default_value" : "default" })' |
| 106 |
|
| 107 |
You can specify string literal instead of column. |
| 108 |
|
| 109 |
.. groonga-command |
| 110 |
.. include:: ../../example/reference/functions/string_slice/usage_string_literal.log |
| 111 |
.. select Memos --output_columns 'string_slice("Groonga", "(roon)(g)", 2)' |
| 112 |
|
| 113 |
Parameters |
| 114 |
---------- |
| 115 |
|
| 116 |
Extraction by position |
| 117 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 118 |
|
| 119 |
There are two required parameters, ``target`` and ``nth``. |
| 120 |
|
| 121 |
There are two optional parameters, ``length`` and ``options``. |
| 122 |
|
| 123 |
``target`` |
| 124 |
~~~~~~~~~~ |
| 125 |
|
| 126 |
Specify a string literal or a string type column. |
| 127 |
|
| 128 |
``nth`` |
| 129 |
~~~~~~~ |
| 130 |
|
| 131 |
Specify a 0-based index number of charactors where to start the extraction from ``target``. |
| 132 |
|
| 133 |
If you specify a negative value, it counts from the end of ``target``. |
| 134 |
|
| 135 |
``length`` |
| 136 |
~~~~~~~~~~ |
| 137 |
|
| 138 |
Specify a number of characters to extract from ``nth``. |
| 139 |
|
| 140 |
The default is 1. |
| 141 |
|
| 142 |
``options`` |
| 143 |
~~~~~~~~~~~ |
| 144 |
|
| 145 |
Specify the following key. |
| 146 |
|
| 147 |
``default_value`` |
| 148 |
Specify a string to be returned when a substring is an empty string except when specifying 0 for ``length``. |
| 149 |
|
| 150 |
The default is an empty string. |
| 151 |
|
| 152 |
Extraction by regular expression |
| 153 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 154 |
|
| 155 |
There are three required parameters, ``target`` and ``regexp`` and ``nth`` or ``name``. Specify either ``nth`` or ``name``. |
| 156 |
|
| 157 |
There is one optional parameter, ``options``. |
| 158 |
|
| 159 |
``target`` |
| 160 |
~~~~~~~~~~ |
| 161 |
|
| 162 |
Specify a string literal or a string type column. |
| 163 |
|
| 164 |
``regexp`` |
| 165 |
~~~~~~~~~~ |
| 166 |
|
| 167 |
Specify a regular expression string. |
| 168 |
|
| 169 |
When you use ``nth`` and specify a value greater than 0, you must use capturing group: ``(subexp)``. |
| 170 |
|
| 171 |
When you use ``name``, you must use named capturing group: ``(?<name>subexp)``, ``(?'name'subexp)``. |
| 172 |
|
| 173 |
``nth`` |
| 174 |
~~~~~~~ |
| 175 |
|
| 176 |
Specify a number of the capturing group for ``regexp``. |
| 177 |
|
| 178 |
A captured string of the ``nth`` capturing group is returned when ``regexp`` is matched to ``target``. |
| 179 |
|
| 180 |
If 0 is specified for ``nth``, the entire string that matches ``regexp`` is returned. |
| 181 |
|
| 182 |
Specify either ``nth`` or ``name``. |
| 183 |
|
| 184 |
``name`` |
| 185 |
~~~~~~~~ |
| 186 |
|
| 187 |
Specify a name of the named capturing group for ``regexp``. |
| 188 |
|
| 189 |
A captured string of the named capturing group that matches ``name`` is returned |
| 190 |
when ``regexp`` is matched to ``target``. |
| 191 |
|
| 192 |
Specify either ``nth`` or ``name``. |
| 193 |
|
| 194 |
``options`` |
| 195 |
~~~~~~~~~~~ |
| 196 |
|
| 197 |
Specify the following key. |
| 198 |
|
| 199 |
``default_value`` |
| 200 |
Specify a string returned if ``regexp`` does not match to ``target``. |
| 201 |
This value also be returned when the value of ``nth`` or ``name`` is incorrect. |
| 202 |
|
| 203 |
The default is an empty string. |
| 204 |
|
| 205 |
Return value |
| 206 |
------------ |
| 207 |
|
| 208 |
``string_slice`` returns a substring extracted under the specified conditions from ``target``. |
| 209 |
|