Python Forum
using the reg expression split to get the words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using the reg expression split to get the words
#1
Hi ,

I am having a string like A(B,C) .

I want to read A B C separately .

I tried below code with class name as "G(A, B)"

 52 if (re.match("[a-zA-Z0-9]\([a-zA-Z0-9]",classname)):
 53            print("++++++++++")
 54            result=re.split("[a-zA-Z0-9]\([a-zA-Z0-9]",classname,0)
 55            print(classname)
 56            print(result)
++++++++++
G(A, B)
['', ', B)']

Trying to know how to fetch G and A from here.

Your inputs would be highly appreciated.
Reply
#2
This looks like XY problem. Could you elaborate what your ultimate goal is?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
If you're already using a regex, I'd just grab them all in one go. If match is successful, then all the match objects can be pulled out of the groups() method.
import re
name = "alpha(beta,gamma)"
m = re.match("(\w+)\((\w+),(\w+)\)",name)
print(m.groups())
Output:
('alpha', 'beta', 'gamma')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular Expression for matching words xinyulon 1 2,167 Mar-09-2022, 10:34 PM
Last Post: snippsat
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,800 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Pass results of expression to another expression cmdr_eggplant 2 2,280 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Read re.split expression NewBeie 2 2,104 Apr-25-2019, 12:12 PM
Last Post: NewBeie
  Compare all words in input() to all words in file Trianne 1 2,766 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Use regular expression to return 5 words before and after target word. steve1040 2 12,960 Sep-28-2017, 08:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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