#๐Ÿ”’ i want it to print like if there was no group here how to

143 messages ยท Page 1 of 1 (latest)

mortal swan
teal obsidianBOT
#

@mortal swan

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.

#

Hey @mortal swan!

Please edit your message to use a code block

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
mortal swan
#
py 
import pyperclip, re

phoneRegex = re.compile((r'([0-9]{3}|(\[0-9])\{3})?(\s|-|\.)?([0-9]{3})(\s|-|\.)?([0-9]{3})(\s|-|\.)?([0-9]{4})'))
mo=phoneRegex.findall(r"(324)-243-234-2423 is a good number not readllly (334)-253-264-2422)")
print(mo)

code

#

i dont want it return me tuples basically

sour hemlock
#

can you paste the code here instead of a screenshot?

sour hemlock
#

ahh whoops

#

so what result do you want? Just the numbers?

mortal swan
#

yeah

#

and not like tuples

sour hemlock
#

don't use findall then

#

use re.search

mortal swan
#

ik it ws possible if i dont used groups too

#

oh

sour hemlock
#

!e

import re

phoneRegex = re.compile((r'([0-9]{3}|(\[0-9])\{3})?(\s|-|\.)?([0-9]{3})(\s|-|\.)?([0-9]{3})(\s|-|\.)?([0-9]{4})'))
#mo=phoneRegex.findall(r"(324)-243-234-2423 is a good number not readllly (334)-253-264-2422)")
mo=phoneRegex.search(r"(324)-243-234-2423 is a good number not readllly (334)-253-264-2422)")
print(mo.group())
teal obsidianBOT
mortal swan
#

it didnt included area code

sour hemlock
#

neither did your findall

mortal swan
#

๐Ÿ’€

#

so how to?

sour hemlock
#

your escapes are backwards

#

you have (\ instead of \(

mortal swan
#

oh wait

sour hemlock
#

the \ will always come first

#

(324)-243-234-2423

#

this also isn't a valid number

#

it should be 3 digits for area code

#

then 3 more, then 4

#

(123)-456-7890

mortal swan
sour hemlock
#

let's back up and rebuild so that it makes sense

#

!e

import re

phoneRegex = re.compile(r'\([0-9]{3}\)-[0-9]{3}-[0-9]{4}')
mo=phoneRegex.search(r"(243)-234-2423 is a good number not readllly (334)-264-2422)")
print(mo.group())
teal obsidianBOT
sour hemlock
#

here's a regex for 3 digits in (), followed by -, then 3 digits, then -, then 4 digits

#

ok now let's add the or back in. I'm also going to change search to re.finditer so we can loop through all matches

#

!e

import re

phoneRegex = re.compile(r'([0-9]{3}|\([0-9]{3}\))-[0-9]{3}-[0-9]{4}')
matches = phoneRegex.finditer(r"(243)-234-2423 is a good number not readllly 334-264-2422)")
for match in matches:
    print(match.group())
teal obsidianBOT
sour hemlock
#

now we're matching with and without () for the area code

mortal swan
sour hemlock
#

You're right

#

we can add the ? after the group now, but we'll run into the issue of the - after the area code

#

so we can create a nested group here for the separator

#

!e

import re

phoneRegex = re.compile(r'(([0-9]{3}|\([0-9]{3}\))?(-)?)[0-9]{3}-[0-9]{4}')
matches = phoneRegex.finditer(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
for match in matches:
    print(match.group())
teal obsidianBOT
sour hemlock
#

basically now we're saying "if there's an area code, capture it and its separator"

#

ok now we can add groups for our other digits

#

if we use the groups() method, we can start to view all the groups we're capturing

#

!e

import re

phoneRegex = re.compile(r'([0-9]{3}|\([0-9]{3}\))?(-)?([0-9]{3})-([0-9]{4})')
matches = phoneRegex.finditer(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
for match in matches:
    print(match.groups())
teal obsidianBOT
sour hemlock
#

the 3rd line here tells us there was no area code

mortal swan
#

!e

import re

phoneRegex = re.compile(r'(([0-9]{3}|\([0-9]{3}\))?(-)?)[0-9]{3}-[0-9]{4}')
matches = phoneRegex.search(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
print(matches.group())

teal obsidianBOT
mortal swan
#

why this

sour hemlock
#

you didn't print

#

group() returns the fully matched string

mortal swan
#

!e

import re

phoneRegex = re.compile(r'(([0-9]{3}|\([0-9]{3}\))?(-)?)[0-9]{3}-[0-9]{4}')
matches = phoneRegex.search(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
print(matches.groups())

teal obsidianBOT
sour hemlock
#

if we want a specific group, we can index it

#

matches.group(1) would give us the first group

#

or, we can do .groups() to see all the returned groups at once

mortal swan
#

!e

import re

phoneRegex = re.compile(r'(([0-9]{3}|\([0-9]{3}\))?(-)?)[0-9]{3}-[0-9]{4}')
matches = phoneRegex.search(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
print(matches.groups())

teal obsidianBOT
sour hemlock
#
import re

phoneRegex = re.compile(r'([0-9]{3}|\([0-9]{3}\))?(-)?([0-9]{3})-([0-9]{4})')
matches = phoneRegex.finditer(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
for match in matches:
    area_code, sep1, first_3, last_4 = match.groups()
#

it can be really useful when you do multiple assignment like this

#

then we end up with variables for each of the groups

mortal swan
#

isnt match.group a string?

sour hemlock
#

yes

mortal swan
#

then how can u assign multiple variable its in list?

sour hemlock
#

groups() is a list tuple

#

group() is a string

mortal swan
#

oh yeah

sour hemlock
#

!e

import re

phoneRegex = re.compile(r'([0-9]{3}|\([0-9]{3}\))?(-)?([0-9]{3})-([0-9]{4})')
matches = phoneRegex.finditer(r"(243)-234-2423 is a good number not readllly 334-264-2422 but also 123-4567")
for match in matches:
    area_code, sep1, first_3, last_4 = match.groups()
    if area_code:
        print(f"The area code is {area_code}")
    else:
        print("No area code")
    print(f"The first 3 digits are {first_3}")
    print(f"The last 4 digits are {last_4}")
teal obsidianBOT
sour hemlock
#

then you can do neat things like this

mortal swan
#

cool

sour hemlock
#

if there's an area code, display it, then display the remaining digit groups

#

in my example I only used - as the separator

#

but now it's fairly trivial to go update it with your other separator options

#

I like keeping them simple in the beginning until I can confirm they work, then I'll increase their complexity as I keep testing

#

instead of trying to write one mega beast of a regex from the start

#

this is how you should approach coding in general

#

keep it simple, test often, then build on that

mortal swan
#

i get a lot of application of it ty

#

but why its showing this ?

sour hemlock
#

if there was no match, then mo will be None

#

it's common to include if mo:

#

only proceed if a match was found

mortal swan
#

oh

sour hemlock
#

that's why finditer worked ok in my example. If there was no match, then matches would be empty

mortal swan
#

but why its not matching

sour hemlock
#

looks like you accidentally inserted a character into your pattern?

mortal swan
#

lol

#

it worked now

mortal swan
# sour hemlock

how to do it with search and whats difference between itr and search?

sour hemlock
#

search only returns the first match in the string

#

if you have many phone numbers in a string and you want all of them, then you don't want search

#

findall returns a string of all matches

#

finditer returns an iterable of match objects

mortal swan
digital kite
mortal swan
#

ok

digital kite
#

So findall finds all the matches and collects them all in a list and returns that. So it does all the work before the for-loop gets any data.

#

finditer yields a match one at a time. The for loop can start working as soon as the first match is found.

#

Foro small amounts of text the difference is negligible.

#

But for big things and open ended things (no finish) you often want to get an iterable, giving you answers as produced.

mortal swan
#

find all did nothing

digital kite
#

For example, imagine a function primes() yielding the primes. There's an infinite number of primes. Can't compute all of them first.

#

You're literally searching the text:

pyperclip.paste()

and not whatever calling pyperclip.paste() would have returned.

mortal swan
#

oh wait

sour hemlock
#

sorry was away, I'm back now

mortal swan
sour hemlock
#

"pyperclip.paste()" and pyperclip.paste() are very different

mortal swan
#

yeah ik

sour hemlock
#

so what "difference" are you asking about?

sour hemlock
#

they mentioned a "generator", which is a form of iterable in python

#

and their example of prime numbers was great. You couldn't have a function that says "Get me all the prime numbers", because that is endless

#

but you could have a function that says "get me the next prime number"

#

finditer works in a similar way

#

it basically performs re.search until it finds a match, and then it basically pauses its searching there

mortal swan
#

tyvm gotcha

sour hemlock
#

and then you can ask it for the next one, and it will resume searching until it finds another one (or reaches the end of the string)

#
import re

text = 'a2bc15d9e'
matches = re.finditer('[a-z]', text)


#

have a look at this basic example

#

it finds all single letters inside this string

#

generators can make use of a special function called next()

#

I don't have to use a for loop, but instead I could just say "what's the first match"

#

!e

import re


text = 'a2bc15d9e'

matches = re.finditer('[a-z]', text)


print(next(matches))
teal obsidianBOT
sour hemlock
#

if I wanted the first 3 matches, I could call next() 3 times

#

!e

import re


text = 'a2bc15d9e'
matches = re.finditer('[a-z]', text)


print(next(matches))
print(next(matches))
print(next(matches))
teal obsidianBOT
sour hemlock
#

!e

import re


text = 'a2bc15d9e'
matches = re.finditer('[a-z]', text)


print(next(matches))
print(next(matches))
print(next(matches))
print(next(matches))
print(next(matches))
print(next(matches))
teal obsidianBOT
# sour hemlock !e ```py import re text = 'a2bc15d9e' matches = re.finditer('[a-z]', text) p...

:x: Your 3.12 eval job has completed with return code 1.

001 | <re.Match object; span=(0, 1), match='a'>
002 | <re.Match object; span=(2, 3), match='b'>
003 | <re.Match object; span=(3, 4), match='c'>
004 | <re.Match object; span=(6, 7), match='d'>
005 | <re.Match object; span=(8, 9), match='e'>
006 | Traceback (most recent call last):
007 |   File "/home/main.py", line 13, in <module>
008 |     print(next(matches))
009 |           ^^^^^^^^^^^^^
010 | StopIteration
sour hemlock
#

look what happens if I call next() too many times

#

there were only 5 matches, but I did next() 6 times

#

This is actually how for loops work under the hood

#

it basically calls next() over and over until it reaches a StopIteration error, then it knows to stop

teal obsidianBOT
#
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.