Skip to content

Commit

Permalink
Refactored replacement elements into base
Browse files Browse the repository at this point in the history
  • Loading branch information
smartycope committed Apr 17, 2024
1 parent 69f49e2 commit ee445b9
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 87 deletions.
25 changes: 24 additions & 1 deletion ezregex/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
from types import EllipsisType
from typing import Callable
from ..EZRegex import EZRegex
from .elements import base
from string import Formatter


def load_base(dialect, exclude=[]) -> dict:
def load_base(dialect, rgroup_func: Callable[[int|str, EllipsisType], str], replace_entire_func=...) -> dict:
rtn = {}
for name, kwargs in base.items():
rtn[name] = EZRegex(**kwargs, dialect=dialect)

# I'm creating this here, so we don't have to reimplement both of them every time
def replace(string, rtn_str=True):
class CustomFormatter(Formatter):
def get_value(self, key, args, kwargs):
return rgroup_func(key, '')

string = CustomFormatter().format(string)

return string if rtn_str else EZRegex(string, dialect, sanatize=False, replacement=True)


rtn['replace'] = replace
rtn['rgroup'] = EZRegex(rgroup_func, dialect, replacement=True)
rtn['replace_entire'] = EZRegex(
lambda cur=...: rgroup_func(0, cur=cur) if replace_entire_func is Ellipsis else replace_entire_func,
dialect,
replacement=True
)

return rtn
2 changes: 1 addition & 1 deletion ezregex/javascript/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Support for the JavaScript dialect of regular expressions"""
__version__ = '1.1.1'
__version__ = '0.0.1'

from .elements import *
from .psuedonymns import *
28 changes: 2 additions & 26 deletions ezregex/javascript/elements.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
# pyright: reportArgumentType = false
import re
from json import load

# pyright: reportUndefinedVariable = false
from ..base import load_base
from ..EZRegex import EZRegex

globals().update(load_base('javascript'))

# TODO: Use https://docs.python.org/3/library/string.html#string.Formatter instead
def replace(string, rtn_str=True):
# Made with ezregex.org
# Previous method
# '{' + optional(anyCharExcept('{'), greedy=False) + group(+alphaNum) + '}' + optional(anyCharExcept('}'), greedy=False)
# Current method
# '{' + group(+alphaNum) + '}'
r = r'\{((?:[A-Za-z0-9_])+)\}'
# Convert them to something unique, then back, so we won't pick up things like {{this}}
# {{this}} -> \0(this\0) -> {this}
# instead of
# {{this}} -> {this} -> \g<this>
string = re.sub('{{', '\0\0(', string)
string = re.sub('}}', '\0\0)', string)
string = re.sub(r, r'\\g<\g<1>>', string)
string = re.sub(r'\0\0\(', '{', string)
string = re.sub(r'\0\0\)', '}', string)
return string if rtn_str else EZRegex(string, 'javascript', sanatize=False, replacement=True)

rgroup = EZRegex(lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>', 'javascript', replacement=True)
replace_entire = EZRegex(lambda cur=...: cur + r'\g<0>', 'javascript', replacement=True)
globals().update(load_base('javascript', lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>'))
2 changes: 1 addition & 1 deletion ezregex/perl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Support for the Perl dialect of regular expressions"""
__version__ = '1.1.1'
__version__ = '0.0.1'

from .elements import *
from .psuedonymns import *
30 changes: 2 additions & 28 deletions ezregex/perl/elements.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
# pyright: reportArgumentType = false
import re
from json import load

# pyright: reportUndefinedVariable = false
from ..base import load_base
from ..EZRegex import EZRegex

globals().update(load_base('perl'))

del UNICODE # type: ignore

# TODO: Use https://docs.python.org/3/library/string.html#string.Formatter instead
def replace(string, rtn_str=True):
# Made with ezregex.org
# Previous method
# '{' + optional(anyCharExcept('{'), greedy=False) + group(+alphaNum) + '}' + optional(anyCharExcept('}'), greedy=False)
# Current method
# '{' + group(+alphaNum) + '}'
r = r'\{((?:[A-Za-z0-9_])+)\}'
# Convert them to something unique, then back, so we won't pick up things like {{this}}
# {{this}} -> \0(this\0) -> {this}
# instead of
# {{this}} -> {this} -> \g<this>
string = re.sub('{{', '\0\0(', string)
string = re.sub('}}', '\0\0)', string)
string = re.sub(r, r'\\g<\g<1>>', string)
string = re.sub(r'\0\0\(', '{', string)
string = re.sub(r'\0\0\)', '}', string)
return string if rtn_str else EZRegex(string, 'perl', sanatize=False, replacement=True)

rgroup = EZRegex(lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>', 'perl', replacement=True)
replace_entire = EZRegex(lambda cur=...: cur + r'\g<0>', 'perl', replacement=True)
globals().update(load_base('perl', lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>'))
2 changes: 1 addition & 1 deletion ezregex/python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
""" Support for the Python dialect of regular expressions"""
__version__ = '0.1.0'
__version__ = '1.1.0'

from .elements import *
from .psuedonymns import *
30 changes: 3 additions & 27 deletions ezregex/python/elements.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
# pyright: reportArgumentType = false
import re
from json import load

# pyright: reportUndefinedVariable = false
from ..base import load_base
from ..EZRegex import EZRegex

globals().update(load_base('python'))

del UNICODE # type: ignore

# TODO: Use https://docs.python.org/3/library/string.html#string.Formatter instead
def replace(string, rtn_str=True):
# Made with ezregex.org
# Previous method
# '{' + optional(anyCharExcept('{'), greedy=False) + group(+alphaNum) + '}' + optional(anyCharExcept('}'), greedy=False)
# Current method
# '{' + group(+alphaNum) + '}'
r = r'\{((?:[A-Za-z0-9_])+)\}'
# Convert them to something unique, then back, so we won't pick up things like {{this}}
# {{this}} -> \0(this\0) -> {this}
# instead of
# {{this}} -> {this} -> \g<this>
string = re.sub('{{', '\0\0(', string)
string = re.sub('}}', '\0\0)', string)
string = re.sub(r, r'\\g<\g<1>>', string)
string = re.sub(r'\0\0\(', '{', string)
string = re.sub(r'\0\0\)', '}', string)
return string if rtn_str else EZRegex(string, 'python', sanatize=False, replacement=True)
globals().update(load_base('python', lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>'))

rgroup = EZRegex(lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>', 'python', replacement=True)
replace_entire = EZRegex(lambda cur=...: cur + r'\g<0>', 'python', replacement=True)
del UNICODE

# Source: http://stackoverflow.com/questions/201323/ddg#201378
email = raw(r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])")
Expand Down
9 changes: 9 additions & 0 deletions tests/data/python_regexs.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This goes ["regex", [things it should match], [things it shouldnt match]]
[
[

],
[

]
]
2 changes: 1 addition & 1 deletion tests/data/regexs.jsonc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This goes [regex, [things it should match], [things it shouldnt match]]
// This goes ["regex", [things it should match], [things it shouldnt match]]
// IMPORTANT NOTE: Because this is a JSON file, and the the regexs get compiled, escaped characters like \n and \t need to
// be double escaped in the regex patterns (i.e. \\n, \\t), but NOT in the strings they should and shouldn't match
[
Expand Down
1 change: 0 additions & 1 deletion tests/test_python.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# import jsonc
from ezregex import python
import jstyleson
import ezregex as er
Expand Down

0 comments on commit ee445b9

Please sign in to comment.