#๐Ÿ”’ regex problem [^\n\s] allows for \n

15 messages ยท Page 1 of 1 (latest)

hollow patrol
#

Hi, so I am using regex to go through a self defined config file.
The config file usually looks like this:

name=Some_name
proj=path/to/proj1
proj=path/to/proj2

then I identify "Some_name" using and the project paths using:

content = file.read()
given_name = re.search(r"(?i:name)\s*=\s*([^\n\s]*)", content).group(1)
project_paths = re.findall(r"(?i:proj)\s*=\s*([^\n\s]*)", content)

and this works great if a name is given. The problem occurs if someone forgets to enter a name, i.e. the file looks like:

name=
proj=path/to/proj1
proj=path/to/proj2

then the variable given_name becomes 'proj=path/to/proj1' instead of an empty string. I'm unsure why the regex expression skips the \n after name=, since no \n should be allowed between the = and the section I'm matching towards.

Or is there a better way to check if someone entered a name in the config file?

sleek lotusBOT
#

@hollow patrol

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.

formal dome
hollow patrol
#

Oh, does \s catch \n too? I didn't know. The idea is to allow users to write smth like
name= Some_name
Without the spaces being included

formal dome
#

If no values can have new line characters (each key is only one line), instead of using regex, you could just split the whole file into lines/read the file line-by-line.

Then for each line, split on only first = you see (.split("=", 1)) and .strip() both sides to remove any starting/ending whitespace

hollow patrol
#

Yeah, maybe that's better. I just fell in love with regex when I started to learn it, so I began using it for almost everything lol. I'll see what I'll do, but thanks! I'll look into that \s* and see how it works more in-depth

formal dome
#

\s is just all whitespace characters, including \n

\s matches any whitespace character (equivalent to [\r\n\t\f\v ])

hollow patrol
#

I didn't even know there were \r, \t, \f or \v lol. Is it possible to write something that means "any \s that is not \n"? I thought \s was only a space

#

oh I think I found it on stackoverflow.
[^\S\r\n] should do the trick. I don't really understand the "return carrier" (\r), but it seems too odd to be allowed

#

thank you for the insight!

sleek shoal
#

Also, because \n is part of \s, [^\n\s] is the same as [^\s] or just \S

hollow patrol
#

but it works! Now I get an empty string and can raise an exception that the name is invalid. Thanks so much for the help!

sleek lotusBOT
#
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.