Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using re.search
#1
If I use the re.search method:

      pattern = r"\\f (.+)\\fr(.+)\\fk(.+)\\ft(.+)\\\+xt(.+)\\\+xt\*(.+)\\f\*"
      fnote=re.search(pattern, irec)
      print(fnote)
I get:
<re.Match object; span=(72, 207), match='\\f + \\fr 3:22 \\fk Ons: \\ft Die meervoudsvorm >

How do I access the span and match items of fnote?
Reply
#2
fnote is a Match object. Their API is documented here
Reply
#3
Thank you, much appreciated.
Reply
#4
Why are you asking questions like this in the forum? You should ask Python.
Output:
> python Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> test_str = "This is my test string" >>> import re >>> results = re.search('test', test_str) >>> type(results) <class 're.Match'> >>> help(results) Help on Match in module re object: class Match(builtins.object) | The result of re.match() and re.search(). | Match objects always have a boolean value of True. | | Methods defined here: | | __copy__(self, /) | | __deepcopy__(self, memo, /) | | __getitem__(self, key, /) | Return self[key]. | | __repr__(self, /) | Return repr(self). | | end(self, group=0, /) | Return index of the end of the substring matched by group. | | expand(self, /, template) | Return the string obtained by doing backslash substitution on the string template, as done by the sub() method. | | group(...) | group([group1, ...]) -> str or tuple. | Return subgroup(s) of the match by indices or names. | For 0 returns the entire match. | | groupdict(self, /, default=None) | Return a dictionary containing all the named subgroups of the match, keyed by the subgroup name. | | default | Is used for groups that did not participate in the match. | | groups(self, /, default=None) | Return a tuple containing all the subgroups of the match, from 1. | | default | Is used for groups that did not participate in the match. | | span(self, group=0, /) | For match object m, return the 2-tuple (m.start(group), m.end(group)). | | start(self, group=0, /) | Return index of the start of the substring matched by group. | | ---------------------------------------------------------------------- | Class methods defined here: | | __class_getitem__(...) from builtins.type | See PEP 585 | | ---------------------------------------------------------------------- | Data descriptors defined here: | | endpos | The index into the string beyond which the RE engine will not go. | | lastgroup | The name of the last matched capturing group. | | lastindex | The integer index of the last matched capturing group. | | pos | The index into the string at which the RE engine started looking for a match. | | re | The regular expression object. | | regs | | string | The string passed to match() or search().
Of these I think "span" is the best fit.
Quote: | span(self, group=0, /)
| For match object m, return the 2-tuple (m.start(group), m.end(group)).
Reply


Forum Jump:

User Panel Messages

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