Python Forum
How to assign a found regex expression to a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to assign a found regex expression to a variable
#1
When I try to assign the regex expression I search for to a variable, I get this:

Quote:>>> studentnrRegex = re.compile(r'(18\d\d\d\d\d\d\d\d)')
>>> studentnr = studentnrRegex.search(text)
>>> studentnr
<_sre.SRE_Match object; span=(2071, 2081), match='1825010243'>

I thought I could just get the student number directly. Not so it seems.

What is the best way to get the actual student number, in this case 1825010243 out of there as a variable?
Reply
#2
studentnr.group()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you, that was what I needed!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 761 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  Regex Include and Exclude patterns in Same Expression starzar 2 791 May-23-2023, 09:12 AM
Last Post: Gribouillis
  Split string using variable found in a list japo85 2 1,300 Jul-11-2022, 08:52 AM
Last Post: japo85
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,529 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  Regex Expression With Code Query In Pandas eddywinch82 8 2,341 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Problem in Regex Expression shantanu97 2 1,714 Sep-28-2021, 03:40 AM
Last Post: shantanu97
  Using Regex Expression With Isin in Python eddywinch82 0 2,291 Apr-04-2021, 06:25 PM
Last Post: eddywinch82
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,365 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,550 Nov-23-2020, 05:15 PM
Last Post: cananb
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,268 Aug-12-2020, 10:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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