Skip to content

Commit

Permalink
Enhance docs related to new flags argument.
Browse files Browse the repository at this point in the history
Related to #4429.
  • Loading branch information
pekkaklarck committed Aug 16, 2022
1 parent cb45caf commit 545c48d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
16 changes: 7 additions & 9 deletions src/robot/libraries/BuiltIn.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,14 +1316,10 @@ def should_match_regexp(self, string, pattern, msg=None, values=True, flags=None
to denote the beginning and end of the string, respectively.
For example, ``^ello$`` only matches the exact string ``ello``.
Possible flags altering how the expression is parsed (e.g.
``re.IGNORECASE``, ``re.MULTILINE``) can be embedded to the
pattern like ``(?im)pattern``. The most useful flags are ``i``
(case-insensitive), ``m`` (multiline mode), ``s`` (dotall mode)
and ``x`` (verbose). Alternatively, RobotFramework 5.1 introduced the
optional ``flags`` argument to specify the flags directly,
i.e. ``flags=MULTILINE`` or ``flags=IGNORECASE|DOTALL``.
All valid Python re.XXX flags are supported.
Possible flags altering how the expression is parsed (e.g. ``re.IGNORECASE``,
``re.MULTILINE``) can be given using the ``flags`` argument (e.g.
``flags=IGNORECASE | MULTILINE``) or embedded to the pattern (e.g.
``(?im)pattern``).
If this keyword passes, it returns the portion of the string that
matched the pattern. Additionally, the possible captured groups are
Expand All @@ -1335,15 +1331,17 @@ def should_match_regexp(self, string, pattern, msg=None, values=True, flags=None
Examples:
| Should Match Regexp | ${output} | \\\\d{6} | # Output contains six numbers |
| Should Match Regexp | ${output} | ^\\\\d{6}$ | # Six numbers and nothing more |
| ${ret} = | Should Match Regexp | Foo: 42 | (?i)foo: \\\\d+ |
| ${ret} = | Should Match Regexp | Foo: 42 | foo: \\\\d+ | flags=IGNORECASE |
| ${ret} = | Should Match Regexp | Foo: 42 | (?i)foo: \\\\d+ |
| ${match} | ${group1} | ${group2} = |
| ... | Should Match Regexp | Bar: 43 | (Foo|Bar): (\\\\d+) |
=>
| ${ret} = 'Foo: 42'
| ${match} = 'Bar: 43'
| ${group1} = 'Bar'
| ${group2} = '43'
The ``flags`` argument is new in Robot Framework 5.1.
"""
res = re.search(pattern, string, flags=parse_re_flags(flags))
if res is None:
Expand Down
37 changes: 29 additions & 8 deletions src/robot/libraries/String.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def get_lines_matching_regexp(self, string, pattern, partial_match=False, flags=
Python regular expression syntax in general and how to use it
in Robot Framework data in particular.
By default lines match only if they match the pattern fully, but
Lines match only if they match the pattern fully by default, but
partial matching can be enabled by giving the ``partial_match``
argument a true value. The value is considered true
if it is a non-empty string that is not equal to ``false``, ``none`` or
Expand All @@ -352,9 +352,10 @@ def get_lines_matching_regexp(self, string, pattern, partial_match=False, flags=
If the pattern is empty, it matches only empty lines by default.
When partial matching is enabled, empty pattern matches all lines.
To make the match case-insensitive, you can prefix
the pattern with case-insensitive flag ``(?i)`` or use the
``flags=IGNORECASE`` argument (new in RobotFramework 5.1)
Possible flags altering how the expression is parsed (e.g. ``re.IGNORECASE``,
``re.VERBOSE``) can be given using the ``flags`` argument (e.g.
``flags=IGNORECASE | VERBOSE``) or embedded to the pattern (e.g.
``(?ix)pattern``).
Lines are returned as one string concatenated back together with
newlines. Possible trailing newline is never returned. The
Expand All @@ -366,9 +367,10 @@ def get_lines_matching_regexp(self, string, pattern, partial_match=False, flags=
| ${ret} = | Get Lines Matching Regexp | ${ret} | (?i)FAIL: .* |
| ${ret} = | Get Lines Matching Regexp | ${ret} | FAIL: .* | flags=IGNORECASE |
See `Get Lines Matching Pattern` and `Get Lines Containing
String` if you do not need full regular expression powers (and
complexity).
See `Get Lines Matching Pattern` and `Get Lines Containing String` if you
do not need the full regular expression powers (and complexity).
The ``flags`` argument is new in Robot Framework 5.1.
"""
if is_truthy(partial_match):
match = re.compile(pattern, flags=parse_re_flags(flags)).search
Expand Down Expand Up @@ -396,6 +398,11 @@ def get_regexp_matches(self, string, pattern, *groups, flags=None):
individual group contents. All groups can be given as indexes (starting
from 1) and named groups also as names.
Possible flags altering how the expression is parsed (e.g. ``re.IGNORECASE``,
``re.MULTILINE``) can be given using the ``flags`` argument (e.g.
``flags=IGNORECASE | MULTILINE``) or embedded to the pattern (e.g.
``(?im)pattern``).
Examples:
| ${no match} = | Get Regexp Matches | the string | xxx |
| ${matches} = | Get Regexp Matches | the string | t.. |
Expand All @@ -410,7 +417,7 @@ def get_regexp_matches(self, string, pattern, *groups, flags=None):
| ${named group} = ['he', 'ri']
| ${two groups} = [('h', 'e'), ('r', 'i')]
The ``flags`` option has been introduced in RobotFramework 5.1.
The ``flags`` argument is new in Robot Framework 5.1.
"""
regexp = re.compile(pattern, flags=parse_re_flags(flags))
groups = [self._parse_group(g) for g in groups]
Expand Down Expand Up @@ -455,11 +462,18 @@ def replace_string_using_regexp(self, string, pattern, replace_with, count=-1, f
information about Python regular expression syntax in general
and how to use it in Robot Framework data in particular.
Possible flags altering how the expression is parsed (e.g. ``re.IGNORECASE``,
``re.MULTILINE``) can be given using the ``flags`` argument (e.g.
``flags=IGNORECASE | MULTILINE``) or embedded to the pattern (e.g.
``(?im)pattern``).
If you need to just remove a string see `Remove String Using Regexp`.
Examples:
| ${str} = | Replace String Using Regexp | ${str} | 20\\\\d\\\\d-\\\\d\\\\d-\\\\d\\\\d | <DATE> |
| ${str} = | Replace String Using Regexp | ${str} | (Hello|Hi) | ${EMPTY} | count=1 |
The ``flags`` argument is new in Robot Framework 5.1.
"""
count = self._convert_to_integer(count, 'count')
# re.sub handles 0 and negative counts differently than string.replace
Expand Down Expand Up @@ -500,6 +514,13 @@ def remove_string_using_regexp(self, string, *patterns, flags=None):
about the regular expression syntax. That keyword can also be
used if there is a need to remove only a certain number of
occurrences.
Possible flags altering how the expression is parsed (e.g. ``re.IGNORECASE``,
``re.MULTILINE``) can be given using the ``flags`` argument (e.g.
``flags=IGNORECASE | MULTILINE``) or embedded to the pattern (e.g.
``(?im)pattern``).
The ``flags`` argument is new in Robot Framework 5.1.
"""
for pattern in patterns:
string = self.replace_string_using_regexp(string, pattern, '', flags=flags)
Expand Down

0 comments on commit 545c48d

Please sign in to comment.