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

Add regex replace custom function #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bin/q.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ def regexp(regular_expression, data):
else:
return False

def resub(data,expression,replace):
if data is None:
return ""
return re.sub(expression,replace,data)

def md5(data,encoding):
m = hashlib.md5()
m.update(six.text_type(data).encode(encoding))
Expand Down Expand Up @@ -211,6 +216,11 @@ def __init__(self,func_type,name,usage,description,func_or_obj,param_count):
"Find regexp in string expression. Returns 1 if found or 0 if not",
regexp,
2),
UserFunctionDef(FunctionType.REGULAR,
"resub","resub(<expr>,regex,replace) = <replaced-expr-string>",
"Find and replace text",
resub,
3),
UserFunctionDef(FunctionType.REGULAR,
"sha","sha(<expr>,<encoding>,<algorithm>) = <hex-string-of-sha>",
"Calculate sha of some expression. Algorithm can be one of 1,224,256,384,512. For now encoding must be manually provided. Will use the input encoding automatically in the future.",
Expand Down