Python Forum
Regular expression: return string, not list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular expression: return string, not list
#1
Hello,

In the following string I want to eliminate ';'.
The code
aaa = 'ABCD;'
bbb = re.findall('[A-z]', aaa)
returns list:
Output:
>>> bbb ['A', 'B', 'C', 'D']
Does exist an operation in Re, that allows to return string, or the only option is to convert list in string ?
Thanks.
Reply
#2
Well, here is a solution:

bbb = re.match('[A-z]+', aaa).group()
Probably exist another one ?
Reply
#3
If you want to get rid of a part of the string, why are you using a regex? Those are for matching strings. Just replace the bits you don't want with an empty string..
Reply
#4
(Jan-14-2021, 11:29 AM)ndc85430 Wrote: If you want to get rid of a part of the string, why are you using a regex? Those are for matching strings. Just replace the bits you don't want with an empty string..
You consider that replacing bits is more efficient than using Re ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 296 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,448 Jan-05-2024, 08:30 PM
Last Post: sgrey
  Regular Expression search to comment lines of code Gman2233 5 1,595 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,603 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,873 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,132 Mar-09-2022, 10:34 PM
Last Post: snippsat
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,037 Jan-09-2022, 02:39 AM
Last Post: Python84
  printing an string instead of a expression Underscore 2 1,781 Oct-11-2021, 03:10 PM
Last Post: deanhystad
  regular expression question Skaperen 4 2,419 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How to invoke a function with return statement in list comprehension? maiya 4 2,753 Jul-17-2021, 04:30 PM
Last Post: maiya

Forum Jump:

User Panel Messages

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