Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] regex issue
#1
Hi

I'm spending too much time in trying to fing how to get the number between 'BEGIN' and 'END' using a pure regex: what is the correct syntax to say 'ignore up to 'BEGIN/' and get until 'END' ?
(Note the number of slash '/' differs from a case to another, so i'm not interested by 'split')

Thanks

Following trials are incomplete
line = '/blabla/bbbb/llll/aaaa/BEGIN/0555END/lalalalalala' get0 = re.findall(r'.*?(?=BEGIN)', line) get1 = re.findall(r'.*?BEGIN\/', line) get2 = re.findall(r'.*?END\/', line) get3 = re.findall(r'^[.*?BEGIN\/]*(.*)END', line) get4 = re.findall(r'.*!(?=BEGIN)*(.*)END', line)
Reply
#2
Something like this.
import re line = '/blabla/bbbb/llll/aaaa/BEGIN/0555END/lalalalalala' # capturing group f_all = re.findall(r'BEGIN/(\d+)END', line) # lookarounds (no capturing group needed) s = re.search(r'(?<=BEGIN/)\d+(?=END)', line) print(f_all) print(s.group(0))
Output:
['0555'] 0555
Reply
#3
seems so easy ... thanks
Reply
#4
Lookarounds make re work harder. I think they are unnecessary here.

import re line2 = '/blabla/bbbb/llll/aaaa/BEGIN/0555END/lala/BEGIN/0555END/lalalala/BEGIN/0555END/blabla/' # lazy search pattern find anything between BEGIN/ and END/ i = re.compile(r'BEGIN/(.*?)END/') res = i.search(line2) # res.group(1) returns '0555' res = i.findall(line2) # res = ['0555', '0555', '0555']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] re.split issue paul18fr 10 942 Nov-14-2025, 06:02 PM
Last Post: deanhystad
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,850 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
Question [SOLVED] Can't figure out right regex Winfried 3 1,644 Mar-02-2025, 05:57 PM
Last Post: Winfried
  regex issue jacksfrustration 4 1,637 Jun-25-2024, 03:07 PM
Last Post: Pedroski55
  issue in using regex akbarza 4 2,043 Nov-14-2023, 10:00 AM
Last Post: buran
  [solved] Regex expression do not want to taken :/ SpongeB0B 2 7,148 Nov-06-2023, 02:43 PM
Last Post: SpongeB0B
  Facing issue in python regex newline match Shr 6 9,931 Oct-25-2023, 09:42 AM
Last Post: Shr
  Help with a regex? (solved) wrybread 3 2,150 May-01-2023, 05:12 AM
Last Post: deanhystad
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 2,796 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 5,245 Nov-16-2022, 01:49 PM
Last Post: carecavoador

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.