[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Python Help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 12
Thread images: 2

File: whatthefuck.png (3KB, 482x103px) Image search: [Google]
whatthefuck.png
3KB, 482x103px
I've got a string coming into the function, and all I need to do here is return true or false depending on the contents.

If you can't tell by my regex, the string should look something like

'2' or '2+ 2' or '4 +2 * 0'

and should not look anything like

'asldkfjasldkfjaslkdfj' or '2 - 3' or '9 / 12'

yet somehow, when those last three strings are passed in, my function is still returning true. What's wrong here?
>>
>>265994

Think about what you're doing with the repetition operator (*). It means "ZERO or MORE". You're basically matching when there are ZERO instances of what you're looking for.
>>
>>265994
Also "\s" is just a stupid way of writing "."
>>
>>265994
Also

if (condition):
__return(true)
return(false)

is an antipattern in any language, not just Python.

Just write

return (condition)
>>
>>266014

\s matches whitespace. The dot matches anything.
>>
>>266008
how can i see if the entire string matches the conditions instead of if a match exists in it
>>
>>266026
It's already doing that, it's just that your regex matches a bunch more whole strings than you intend to.
>>
>>266032
can you tell me how it's matching strings like "asdasdfhasdf" and "2/3"? I've been using an online tool to check and it doesnt work on the tool
>>
>>266043
You need to nail it down with ^ and $.

Go work through regexone.com .
>>
>>266026

You don't understand the repetition operator. It matches ZERO OR MORE. Think about this:

Suppose you're trying to match the letter A and you do:

A*

Now think about what this does. It matches A zero or more times. So it will match in the following instances:

A
AA
B ( it matches once because it found the letter A zero times)

Now you understand what your problem is? Turn the repitition operator from * (zero or more times) to + (one or more times)
>>
File: firefox_2017-02-13_17-13-48.png (11KB, 588x325px) Image search: [Google]
firefox_2017-02-13_17-13-48.png
11KB, 588x325px
>>266068

Use this page to test your regular expressions: http://regexpal.com.s3-website-us-east-1.amazonaws.com/

Now you can do this. It will match what you're looking for. Like >>266064 said, you might want to use ^ and $ to match the string exactly.
>>
>>266064
>>266073
^ and $ Worked, thanks
Thread posts: 12
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.