#๐Ÿ”’ Confused

38 messages ยท Page 1 of 1 (latest)

bleak hemlock
bright elbowBOT
#

@bleak hemlock

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

bleak hemlock
#

Any idea on what went wrong here?

stable cobalt
# bleak hemlock

\u, which you have in that string, is a special escape character, similar to \n. It expects a number after the u to indicate a Unicode character of a specific code.

If you want \ literally in the string, you need to either escape them by changing them all to \\, or by making the string they're in a raw string by prefixing the string with an r: r'<\u>'.

bleak hemlock
#

i get what the error is

#

but wdym by prefixing the string

stable cobalt
#

Look at the example I gave at the end. Note the r before the opening ' quote.

bleak hemlock
#

but wouldn't that register in python as regex

stable cobalt
#

r'This is a raw string'

bleak hemlock
#

oh wait nvm

#

that has no effect

stable cobalt
#

No. r means raw. It's just sometimes used when creating regexes because regexes can contain backslashes.

#

It will have an effect.

#

Making it a raw string will fix the current error.

bleak hemlock
#

i always use r

#

before inputting a regex pattern into python

#

so that's why my mind was confused when you suggested that

stable cobalt
#

Ah. They're unrelated ideas that both just happen to start with R.

chrome wagon
# bleak hemlock before inputting a regex pattern into python

Just to clarify a bit: the r isn't for "regexp", it's for "raw". We use raw strings for both regexps and Windows paths-containing-backslashes to avoid Python interpreting the backslashes itself, instead passing them through into the string unchanged for use by whatever wants the (the regexp or windows path).

bleak hemlock
plucky roost
# bleak hemlock

the pattern should probably be r"</?[A-Za-z]+>" as the optional / should be the first character within the angled brackets and ? is not a valid character within a tag name

#

within the square brackets in a regex most characters are without special meaning (there are some exceptions though)

bleak hemlock
bleak hemlock
#

so that's why i kept it

plucky roost
# bleak hemlock but it worked

it may seem like it works as it should at first glance, but your version will also accept tags that aren't valid tags in this string "<d/iv> <di?v>" and so on

#

i don't think that this is something you want, right?

bleak hemlock
#

i will fix it then

#

Thanks

plucky roost
plucky roost
#

it's from mistakes you learn the most

bright elbowBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.