Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8090583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:46:21+00:00 2026-06-05T19:46:21+00:00

I try to understand the regex in python. How can i split the following

  • 0

I try to understand the regex in python. How can i split the following sentence with regular expression?

"familyname, Givenname A.15.10"

this is like the phonebook in python regex http://docs.python.org/library/re.html. The person maybe have 2 or more familynames and 2 or more givennames. After the familynames exist ‘, ‘ and after givennames exist ”. the last one is the office of the person. What i did until know is

 import re
 file=open('file.txt','r')
 data=file.readlines()
 for i in range(90):
person=re.split('[,\.]',data[i],maxsplit=2)
print(person)

it gives me a result like this

 ['Wegner', ' Sven Ake G', '15.10\n'] 

i want to have something like

 ['Wegner', ' Sven Ake', 'G', '15', '10']. any idea?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:46:23+00:00Added an answer on June 5, 2026 at 7:46 pm

    In the regex world it’s often easier to “match” rather than “split”. When you’re “matching” you tell the RE engine directly what kinds of substrings you’re looking for, instead of concentrating on separating characters. The requirements in your question are a bit unclear, but let’s assume that

    • “surname” is everything before the first comma
    • “name” is everything before the “office”
    • “office” consists of non-space characters at the end of the string

    This translates to regex language like this:

    rr = r"""
        ^         # begin
        ([^,]+)   # match everything but a comma
        (.+?)     # match everything, until next match occurs
        (\S+)     # non-space characters
        $         # end
    """
    

    Testing:

    import re
    rr = re.compile(rr, re.VERBOSE)
    print rr.findall("de Batz de Castelmore d'Artagnan, Charles Ogier W.12.345")
    # [("de Batz de Castelmore d'Artagnan", ', Charles Ogier ', 'W.12.345')]
    

    Update:

    rr = r"""
        ^         # begin
        ([^,]+)   # match everything but a comma
        [,\s]+    # a comma and spaces
        (.+?)     # match everything until the next match
        \s*       # spaces
        ([A-Z])   # an uppercase letter
        \.        # a dot
        (\d+)     # some digits
        \.        # a dot
        (\d+)     # some digits
        \s*       # maybe some spaces or newlines
        $         # end
    """
    
    import re
    rr = re.compile(rr, re.VERBOSE)
    s = 'Wegner, Sven Ake G.15.10\n' 
    print rr.findall(s)
    # [('Wegner', 'Sven Ake', 'G', '15', '10')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to understand lexical-scoping. In lexical-scoping, I have this code, C like syntax:
I try to understand how Protovis works, and I stumbled upon code like this:
I'm using the following tester to try and figure out this regex: http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx My
I can fix the below exception with a try-catch loop but I cannot understand
there is one think, i can't understand anyway:((( when i try to set cookie(it
I try to understand what a Model is. I already read this answer .
This is a great regular expression for dates... However it hangs indefinitely on this
Try to understand WPF. This is my test classes: public partial class MainWindow :
i try to understand how i can show my textfield over the keyboard and
In my Python application, I need to write a regular expression that matches a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.