#๐Ÿ”’ Regular Expressions

118 messages ยท Page 1 of 1 (latest)

slate ivy
#

Working on A code in Regular Expressions
The Problem Statement is to check if a Mobile numbe is Valid or Not based on :-

  1. We want An 11 or 12 Digit Number
    11 if it starts with 0
    12of it starts with 91
  2. We want the 3rd Digit to be either **6 or 7 or 8 or 9 **so from 6-9
  3. We want the remaining 9 digits anything from 0-9
    Our Final Number should have 11 or 12 Digits only

Attaching Pastebin link for the Code
https://pastebin.com/GR7DwntR

burnt sluiceBOT
#

@slate ivy

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.

agile rover
#

it should start with 6-9 but also 91?

#

how is that possible?

#

are there examples?

slate ivy
#

9169876543210

#

Uhh sorry I copy posted it without correcting it

#

We want 12 or 11 Digit Number

#

Either Starting with 91 or 0

#

Then the third digit should be 6 or 7 or 8 or 9

#

Followed by the remaining last 9 digits that can be anything from 0-9

#

So

  1. We want An 11 or 12 Digit Number
    11 if it starts with 0
    12 of it starts with 91
  2. We want the 3rd Digit 6 or 7 or 8 or 9
  3. We want the remaining 9 digits anything from 0-9
    Our Final Number should have 11 or 12 Digits only
slate ivy
agile rover
#

when you want an "or" situation in regex, you use |

#

so starting with a 0 or 91 would be (0|91)

slate ivy
#

Could you help me with the Rest of it too

agile rover
#

you basically have it, but you've thrown a lot of extra () at it for some reason

#

you can remove just about all of those

#

other than the ones around (0|91)

slate ivy
#

I get it

#

My issue is that I can't seem to be able to Hold it True when I want it for Exact 11 or 12 Digits

#

Like if you see the Number I have taken it Ends with 000

#

That being the case it should be False so My output should Invalid

agile rover
#

you can "anchor" a string to the start or end by using ^ and $

#

or you can use re.match instead (which basically does the anchoring for you)

slate ivy
#

I have done as you've said

#

I think it's matching the conditions till it finds true

#

Like you go grab a chocolate at a Departmental store

agile rover
#

re.search will search the entire string for a pattern.
re.match ensures the ENTIRE string is the pattern

slate ivy
#

Only in My case the store should only have a chocolate

#

Is that so ?

agile rover
#

let's see your updated code

slate ivy
agile rover
#

you still have extra () in there

#

it should be (0|91) to check for either of those at the start of the string

#

followed by [6-9]

#

followed by [0-9]{9}

#

which can be simplified to \d{9}

#

you don't need every single character in []

#

don't do [0], don't do [9]

slate ivy
#

Got You

#

like this ---->"(0|91)[6-9]\d{9}"

agile rover
#

exactly ๐Ÿ™‚

slate ivy
#

done that

agile rover
#

and then either re.match, or re.search but with ^(0|91)[6-9]\d{9}$

slate ivy
#

I have simplified the code as you've said

agile rover
#

and is it not working?

slate ivy
#

It works for case 2---> re.search with ^(0|91)[6-9]\d{9}$

#

but for case 1

agile rover
#

does it show which cases fail?

slate ivy
#

this is what it shows

#

My Output should be invalid right

agile rover
#

!e

import re

num = '91612345678900'

print(re.match(r'^(0|91)[6-9]\d{9}$', num))
burnt sluiceBOT
agile rover
#

something is funky with that test

#

because it doesn't match here

broken lark
#

$ missing

agile rover
#

ahh yeah I was looking at their commented line, haha

#

also sorry I was slightly wrong about re.match. It doesn't check that the entire string is a match. It just anchors from the start

slate ivy
#

Okay Got it

#

so i need to include the '$' sign at the end in re.match

#

and for re.search i need to include the ^ sign at the beginning of the pattern

broken lark
#

both ^ and $ in re.search

agile rover
#

if you use both ^ and $, then it shouldn't really matter which one you use

broken lark
#

or re.fullmatch also exists

slate ivy
#

Yess Thank you so Much

#

Thank You So Much @agile rover and @broken lark

#

I implemented both cases

slate ivy
#

Done โœ… Implemented

#

@broken lark Were you saying something

broken lark
#

not really. except I may have wanted to comment that re.search and re.match have some difference in behaviour of the ^ only when "multiline" mode is used. But since you're not using that, I thought it wasn't worth commenting it ๐Ÿ™‚

slate ivy
#

Ohh

#

Could You recommend me few books and learning resources for Python

#

I am quite a newbie to programming

broken lark
#

!resources

burnt sluiceBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

broken lark
#

I don't really know any specific resource myself, as I acquired stuff over the years from all sorts of sources

slate ivy
#

In the sense that I was on a hiatus for about 2 years and have recently started to learn programming once again

slate ivy
#

I may have a surface level perspective but your Knowledge seems pretty thorough

#

That doesn't just come from here and there

#

You're good That's True

broken lark
#

yea sure, but any resource I used when I was a beginner, was during a time where python 3 didn't even exist yet.. probably. ๐Ÿ˜„

slate ivy
#

Really !!!

broken lark
#

the most common resource I use is just the regular python docs.

#

(or the docs of any library I use)

slate ivy
#

That's like more than 15 years ago

#

U are a seasoned pro in that cade

broken lark
#

sounds about right, year-wise.

slate ivy
#

The documentation there

broken lark
slate ivy
#

Ohh I was referring that today for RE

#

It was good

#

Would Definitely appreciate a cleaner UI though

#

Is it hard to transition from 2.0 to 3.0

broken lark
#

it's one of the cleanest and nicest docs I've ever seen, honestly ๐Ÿ˜„

agile rover
#

Pretty much all regex I've picked up has been from helping people out in these threads

#

I rarely use it personally

agile rover
#

but being curious enough to want to know the answer

broken lark
#

same for me with stuff like pandas and pygame, and so on

slate ivy
#

My first time using that documentation

agile rover
#

regex101 is also a great resource

slate ivy
#

I really hope to learn from you

#

Thank You So Much Both of You @broken lark @agile rover

#

Bye Bye

burnt sluiceBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.