Python Forum
Regex: How to say 'any number of characters of any type until x'?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex: How to say 'any number of characters of any type until x'?
#1
I want to be able to refer to any number of characters of any type (including special characters) up until a specified character, which we'll call 'x' here.

So for example: wdq3jdbmhb..s?//seuybwbxqwhdbi3

I would like to isolate: wdq3jdbmhb..s?//seuybwb
Reply
#2
You can try something like this.
>>> import re
>>> 
>>> s = 'wdq3jdbmhb..s?//seuybwbxqwhdbi3'
>>> r = re.search(r'(^(.*?))(x)', s)
>>> r.group(1)
'wdq3jdbmhb..s?//seuybwb'
Reply
#3
Something like:
>>> s
'wdq3jdbmhb..s?//seuybwbxqwhdbi3'
>>> match = re.match(r".*x", s)
>>> s[:match.end()]
'wdq3jdbmhb..s?//seuybwbx'
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find if chain of characters or number Frankduc 4 1,789 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  Regex not finding all unicode characters tantony 3 2,269 Jul-13-2021, 09:11 PM
Last Post: tantony
  Please support regex for version number (digits and dots) from a string Tecuma 4 3,152 Aug-17-2020, 09:59 AM
Last Post: Tecuma
  [regex] Good way to parse variable number of items? Winfried 4 2,600 May-15-2020, 01:54 PM
Last Post: Winfried
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,629 May-15-2020, 01:37 PM
Last Post: snippsat
  Type hinting - return type based on parameter micseydel 2 2,458 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Unable to pull number using BeautifulSoup and Regex sarath_unrelax 2 1,898 Dec-18-2019, 08:09 PM
Last Post: sarath_unrelax
  Split Column Text by Number of Characters cgoldstein 3 2,984 Mar-11-2019, 01:45 PM
Last Post: perfringo
  [Regex] Findall returns wrong number of hits Winfried 8 5,782 Aug-23-2018, 02:21 PM
Last Post: Winfried
  Counting number of characters in a string Drone4four 1 3,433 Aug-16-2018, 02:33 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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