On Mon, Mar 27, 2017 at 05:17:40PM +0200, Simon D. wrote:
> The regexp string litteral could be represented by : re""
>
> It would ease the use of regexps in Python, allowing to have some regexp
> litterals, like in Perl or JavaScript.
>
> We may end up with an integration like :
>
> >>> import re
> >>> if re".k" in 'ok':
> ... print "ok"
> ok
I dislike the suggested syntax re".k". It looks ugly and not different
enough from a raw string. I can easily see people accidentally writing:
if r".k" in 'ok':
...
and wondering why their regex isn't working.
Javascript uses /regex/ as a literal syntax for creating RegExp objects.
That's the closest equivalent to the way Python would have to operate,
although I don't think we can use the /.../ syntax without breaking the
rule that Python's parser will not be more complex than LL(1). So I
think /.../ is definitely out.
Perl 6 uses m/regex/ and a number of other variations:
https://docs.perl6.org/language/regexes
I doubt that this will actually be useful. It *seems* useful if you just
write trivial regexes like your example, but without Perl's rich set of
terse (cryptic?) operators, I don't know that literal regexes
makes enough difference to be worth the trouble. There's not very
much difference between (say) these:
mo = re.search(r'.k', mystring)
if mo:
print(mo.group())
mo = re.'.k'.search(mystring)
if mo:
print(mo.group())
You effectively save two parentheses, that's all. That doesn't seem like
much of a win for introducing new syntax. Can you show some example code
where a regex literal will have a worthwhile advantage?
> Regexps are part of the language in Perl, and the rather complicated
> integration of regexp in other languages, especially in Python, is
> something that comes up easily in language comparing discussion.
Surely you are joking?
Regex integration in Python is simple. Regular expression objects are
ordinary objects, like lists and dicts and floats. The only difference
is that you don't call the Regex object constructor directly, you either
pass a string to a module level function
re.match(r'my regex', mystring)
or you create a regex object:
regex = re.compile(r'my regex')
regex.match(mystring)
That's very neat, Pythonic and simple. The regex itself is very close to
the same syntax uses by Perl, Javascript or other variations, the only
complication is that due to Python's escaping rules you should use a raw
string r'' instead of doubling up all backslashes. I wouldn't call that
"rather complicated" -- it is a lot less complicated than Perl:
- m// can be abbreviated //
- when do you use // directly and when do you use qr// ?
- s/// operator implicitly defines a regex
In Perl 6, I *think* they use rx// instead of qr//, or are they
different things? Both m// and the s/// operator can use arbitrary
delimiters, e.g. ! or , (but not : or parentheses) instead of the
slashes, and m// regexes will implicitly match against $_ if you don't
explicitly match against something else.
Compared to Perl, I don't think Python's regexes are complicated.
--
Steve
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/