Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding text_replace_regex functionality #193

Merged
merged 2 commits into from Oct 28, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/cuisine.py
Expand Up @@ -531,6 +531,20 @@ def text_replace_line(text, old, new, find=lambda old, new: old == new, process=
res.append(line)
return eol.join(res), replaced

def text_replace_regex(text, regex, new, **kwargs):
"""Replace lines that match with the regex returning the new text

Returns: text

`kwargs` is for the compatibility with re.sub(),
then we can use flags=re.IGNORECASE there for example.
"""
res = []
eol = text_detect_eol(text)
for line in text.split(eol):
res.append(re.sub(regex, new, line, **kwargs))
return eol.join(res)

def text_ensure_line(text, *lines):
"""Ensures that the given lines are present in the given text,
otherwise appends the lines that are not already in the text at
Expand Down