Hello, thank you for taking the time!
Here in the curriculum I currently am:
Can’t figure out why there are double backslashes in the excersice:
The excercise test string :
'A message from firstname.lastname@example.org to email@example.com about meeting @2PM'
The excercise Regex :
Thought it is a mistake and will search for literal backslash rather than non-spaces.
Checked it out in python aaaaand… it’s worked:
so in order to investigate the phenomenon I opened regex101 site, aaaaand…
it was actually agreeing with my first assumptions, and didn’t find any matches.
Can anyone shed light why it doesn’t work in theory but will work in practice?
Thank you! and nice trick to know- that ‘r’…
But notice that the regex actually matches “firstname.lastname@example.org” and “email@example.com”! (see terminal prtsc)
As though “\\S” (computer-lang) is interpreted as “non-space-character”(humans-lang), and not like “back-slash followed by capital s” as you told it should.
The question in the challenge seem to predict this behaviour, unlike what I expected after watching the video lecture, or by using “regex101” tool.
Sorry, I edited that part of my response . it’s a double back-slash. now you got my question?
If I got you right, the regex
matches the string
and not the string:
It’s turn-out not to be the case, and that is what my question is about,
That is my point! look above, in the black print-screen, what happend when I run python itself in terminal- it matches “firstname.lastname@example.org”! and that also the right answer in the challenge!
Feel free to abandon me if I’m too tiresome …
python terminal will match the said email adressess wether I use
and that make no sense to me.
Now, you said that there are 2 ways to make backslashes to be treated literarely.
- prefixing ‘r’
- writind double backslashes instead of single.
That to assume that I want to make them be treated literarely.
But my goal is to match the fictional email adressess which contain no backslashes, in order to do that I want them to be treated unliterarely which means
- no prefixing ‘r’
- no double ‘backslashes’
So why will it match even with the use of double-backslashes?!
And why are you saying that
If you do not include the
r in front, then to accomplish the same thing, you must write it as:
to match the strings “email@example.com” and “firstname.lastname@example.org”
But according to your first reply
The reason you see the extra
\ is you need to escape the
\ to have it interpret it as a literal
\ , so that together with the
S after it, it is like
\S as you would normally write.
It will make backslashes to be treated literarely, and hence, to not match the said email adressess, which is opposite to my goal?
I said it because that is what happening not because that is what you said
And I dont get you, why
and not the opposite
if want to match non-spaces rather then backslashes?
So why single backslash, no ‘r’ will work too?
Here, I use both single and double backslashes regexes,
both of them will match the said emails.
Well, regexes can really be a one of a headache!
The probalem is probably that the general python interpreter treat the escapes before the re libarary can see the regex or something. played with that a little bit, got just more confussed:
Here I included several backslashes in a string.
- for regex of four(!) backslashes it will find two backslashes per one real backslash in the string,
- for regex of three, Python will send error " unterminated string literal "
- for regex of two (the ‘normal’ one) re libarary will send very long trace-back and finally " bad escape " error.
I really appreciate you patience and help ( already beforehand admires your activity in the forum ), best regards!
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.