Python Forum
'|' character within Regex returns a tuple?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'|' character within Regex returns a tuple?
#1
Hi,
Using the '|' character within a Regex is giving me an undesirable result that I have been unable to avoid. For example, consider a 2-page file with the following text in each page:

Page 1:
111A111 #red.

Page 2:
AAA1AAA #green.

for i in range(0,2):
    text = doc.getPage(i).extract_text()

    color_re = re.compile(r'#\w+\.')
    color = color_re.findall(text)
    print(color)
Output:
['red.'] ['green.']
    pattern_re = re.compile(r'(\w+\d+\w+)|(\d+\w+\d+)')
        pattern = pattern_re.findall(text)
        print(pattern)
Output:
('', 'AAA1AAA') ('111A111', '')

If I do:
color =[item.strip('.') for item in color]
I get rid of '.' so, all is good.

But if I do:
pattern = [item.strip(' , ') for item in pattern]
I get the error:
Output:
AttributeError: 'tuple' object has no attribute 'strip'
Is there a way to avoid this error? I need to get rid of the spaces and commas in 'pattern'.
Thanks and apologies in advance if the question is not properly formulated. I'm a beginner.
Reply


Messages In This Thread
'|' character within Regex returns a tuple? - by pprod - Feb-19-2021, 04:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex: a string does not starts and ends with the same character Melcu54 5 2,458 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,461 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,874 Nov-04-2020, 11:26 AM
Last Post: Aggam
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,216 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Regex won't replace character with line break Tomf96 2 2,589 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,781 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Substitution with regular expression returns hidden character SOH bajacri 2 3,855 Nov-17-2019, 03:38 AM
Last Post: bajacri
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,250 May-21-2019, 11:39 AM
Last Post: avorane
  Replace changing string including uppercase character with lowercase character silfer 11 6,257 Mar-25-2019, 12:54 PM
Last Post: silfer
  [Regex] Findall returns wrong number of hits Winfried 8 5,865 Aug-23-2018, 02:21 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020