Python Forum
maximum lenght of a regex-match
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
maximum lenght of a regex-match
#1
You will see three snippets, first a part of my Python-code, second a part of the text the Python works on and third some printed results. Then I describe my problem

<snippet>
else:
print('Tak1')
RegString = Rol + r'[:]?' + '[ \t\n]*' + RuweNaam + '[\n\t ]*' + '([A-z]*[:]?[\n\t ]*[A-z, ]*\n)*' + 'Rol[:]?' + '[\n\t ]*' + '(?P<R>[A-Z][a-z]*[ ]?)*\n'
# Hoe vind je de waarde in content
# print(RegString)
Reg = re.compile( RegString )
# print(Reg)
Match = Reg.search( content )
print(Match)
Role = Match.group('R').strip()
print(Role)
#
<\snippet>

<snippet>
Personen

Naam: Mulmaker, Helena
Rol: Getuige

Naam: Lutke, Dorothea
Geslacht: v
Rol: Moeder

Naam: Mulmaker, David
Geslacht: m
Rol: Vader

<\snippet>

Some results from print:
<snippet>
<_sre.SRE_Match object; span=(159, 398), match='Naam: \tMulmaker, Helena\nRol: \tGetuige\n\nNaam:>
<\snippet>
<snippet>
<_sre.SRE_Match object; span=(198, 398), match='Naam: \tLutke, Dorothea\nGeslacht: \tv\nRol: \tMo>
<\snippet>

Problem:
As you can see the match cuts off after a certain length has reached, but in the search a longer match is expected. I hope that the maximum length a match can be made longer.


Questions:
Can this maximum length been set?
Or is it a system-value that cannot been changed by users?
If so, do you have suggestions to bypass this problem?

Thanks, Maashoeven
Reply
#2
My problem can be shown much easier as:

import re
import os
content = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"

RegString = '[0-9]*'
Reg = re.compile(RegString)
Match = Reg.search(content)
print(Match)
The result of this program is:
Output:
<_sre.SRE_Match object; span=(0, 90), match='1234567890123456789012345678901234567890123456789>
As you can see the span and the match should be 90 characters, but in reality the match has a length of only 59 characters.

The problem I am working on can demand a length of say 250-300 characters. Is it possible to change the configuration of Python, so it can handle this demand?
Reply
#3
what you get is the representation of Match object, not the text that you are searching for.
if you need the text, use group method of the Match object
 
import re
content = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"

reg_string = '[0-9]*'
reg = re.compile(reg_string)
my_match = reg.search(content)
print(my_match.group())
by the way, from the representation you can see that span=(0, 90)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing issue in python regex newline match Shr 6 1,277 Oct-25-2023, 09:42 AM
Last Post: Shr
  Failing regex, space before and after the "match" tester_V 6 1,180 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Regex pattern match WJSwan 2 1,244 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Match substring using regex Pavel_47 6 1,421 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Match key-value json,Regex saam 5 5,406 Dec-07-2021, 03:06 PM
Last Post: saam
  regex.findall that won't match anything xiaobai97 1 2,010 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE
  Creating new list based on exact regex match in original list interjectdirector 1 2,281 Mar-08-2020, 09:30 PM
Last Post: deanhystad
  regex match in a string batchen 4 3,202 Jan-20-2020, 08:48 AM
Last Post: batchen
  Regex: Remove all match plus one char before all Alfalfa 34 22,340 Mar-27-2017, 03:41 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

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