|
1 | | -<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.515 2010/06/0302:06:10 momjian Exp $ --> |
| 1 | +<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.516 2010/06/0314:40:42 momjian Exp $ --> |
2 | 2 |
|
3 | 3 | <chapter id="functions"> |
4 | 4 | <title>Functions and Operators</title> |
@@ -3445,19 +3445,22 @@ regexp_replace('foobarbaz', 'b(..)', E'X\\1Y', 'g') |
3445 | 3445 | </para> |
3446 | 3446 |
|
3447 | 3447 | <para> |
3448 | | - The <function>regexp_matches</> function returnsall of the captured |
3449 | | - substrings resulting from matching a POSIX regular expression pattern. |
3450 | | - It has the syntax |
| 3448 | + The <function>regexp_matches</> function returnsa text array of |
| 3449 | +all of the capturedsubstrings resulting from matching a POSIX |
| 3450 | +regular expression pattern.It has the syntax |
3451 | 3451 | <function>regexp_matches</function>(<replaceable>string</>, <replaceable>pattern</> |
3452 | 3452 | <optional>, <replaceable>flags</> </optional>). |
3453 | | - If there is no match to the <replaceable>pattern</>, the function returns |
3454 | | - no rows. If there is a match, the function returns a text array whose |
| 3453 | + The function can return no rows, one row, or multiple rows (see |
| 3454 | + the <literal>g</> flag below). If the <replaceable>pattern</> |
| 3455 | + does not match, the function returns no rows. If the pattern |
| 3456 | + contains no parenthesized subexpressions, then each row |
| 3457 | + returned is a single-element text array containing the substring |
| 3458 | + matching the whole pattern. If the pattern contains parenthesized |
| 3459 | + subexpressions, the function returns a text array whose |
3455 | 3460 | <replaceable>n</>'th element is the substring matching the |
3456 | 3461 | <replaceable>n</>'th parenthesized subexpression of the pattern |
3457 | 3462 | (not counting <quote>non-capturing</> parentheses; see below for |
3458 | | - details). If the pattern does not contain any parenthesized |
3459 | | - subexpressions, then the result is a single-element text array containing |
3460 | | - the substring matching the whole pattern. |
| 3463 | + details). |
3461 | 3464 | The <replaceable>flags</> parameter is an optional text |
3462 | 3465 | string containing zero or more single-letter flags that change the |
3463 | 3466 | function's behavior. Flag <literal>g</> causes the function to find |
@@ -3487,6 +3490,16 @@ SELECT regexp_matches('foobarbequebaz', 'barbeque'); |
3487 | 3490 | ---------------- |
3488 | 3491 | {barbeque} |
3489 | 3492 | (1 row) |
| 3493 | +</programlisting> |
| 3494 | + </para> |
| 3495 | + |
| 3496 | + <para> |
| 3497 | + It is possible to force <function>regexp_matches()</> to always |
| 3498 | + return one row by using a sub-select; this is particularly useful |
| 3499 | + in a <literal>SELECT</> target list when you want all rows |
| 3500 | + returned, even non-matching ones: |
| 3501 | +<programlisting> |
| 3502 | +SELECT col1, (SELECT regexp_matches(col2, '(bar)(beque)')) FROM tab; |
3490 | 3503 | </programlisting> |
3491 | 3504 | </para> |
3492 | 3505 |
|
|