5 examples of 'python split regex' in Python

Every line of 'python split regex' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
73def make_regex_split(pattern):
74 try:
75 regex = re.compile(pattern)
76
77 except Exception as e:
78 raise argparse.ArgumentTypeError(
79 "Invalid regular expression '%s'" % (pattern)
80 )
81
82 # This separates regex parsing from application and provides
83 # a nice type factory for argparse
84 def split(line):
85 return regex.split(line)
86
87 return split
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

89def token_split(text, pattern, advanced=False):
90 """
91 @return: An iterator that generates tokens and the gaps between them
92 """
93
94 if advanced:
95 regexp = _compile(pattern) # pattern contains ()
96 else:
97 regexp = re.compile(pattern)
98
99 # If it's a single string, then convert it to a tuple
100 # (which we can iterate over, just like an iterator.)
101 if type(text) is str: text = (text,)
102
103 # Process each substring returned by the iterator, in turn.
104 # "leftover" is used to record any leftover material when we
105 # move on to a new substring.
106 leftover = ''
107 offset = 0
108 for substring in text:
109 position = 0 # The position within the substring
110
111 # Skip any matching material in the substring:
112 match = regexp.match(substring)
113 if match:
114 yield leftover+substring[position:match.start()]
115 yield substring[match.start():match.end()]
116 position = match.end()
117 leftover = ''
118
119 # Walk through the substring, looking for matches.
120 while position < len(substring):
121 match = regexp.search(substring, position)
122 if match:
123 yield leftover+substring[position:match.start()]
124 yield substring[match.start():match.end()]
125 position = match.end()
126 leftover = ''
127 else:
128 leftover = substring[position:]
129 break
130
131 # Update the offset
132 offset += position
133
134 # If the last string had leftover, then return it.
135 if leftover:
136 yield leftover
312def split(pattern, string, maxsplit=0, flags=0, concurrent=None, **kwargs):
313 """Split the source string by the occurrences of the pattern, returning a
314 list containing the resulting substrings. If capturing parentheses are used
315 in pattern, then the text of all groups in the pattern are also returned as
316 part of the resulting list. If maxsplit is nonzero, at most maxsplit splits
317 occur, and the remainder of the string is returned as the final element of
318 the list."""
319 return _compile(pattern, flags, kwargs).split(string, maxsplit, concurrent)
896def _splitShellPattern(value):
897 return value.split(',') if '{' not in value else [value]
63def _tokenize(s):
64 token_spec = [
65 ('HYPHEN', r'-'),
66 ('COORD', r'[0-9,]+(\.[0-9]*)?(?:[a-z]+)?'),
67 ('OTHER', r'.+')
68 ]
69 tok_regex = r'\s*' + r'|\s*'.join(
70 r'(?P<%s>%s)' % pair for pair in token_spec)
71 tok_regex = re.compile(tok_regex, re.IGNORECASE)
72 for match in tok_regex.finditer(s):
73 typ = match.lastgroup
74 yield typ, match.group(typ)

Related snippets