-1

so i am using re.search function to search for a substring, problem is at the end of the string there is repeating data and i only want to search to the first dataset declared

here is the code

file = open ("flash-ori", "rb").read().hex()

DTC_data = re.search("0080040004000100(.*)010202010202020202020202", file)

print (DTC_data.group())

here is what i get

0080040004000100**DATA**01020201020202020202020202020202010202020102020202010202020202020202020102020202020a0202020202020202020202020a02020102020202020202020202020202020202020202020202020202020202020202020101010102020101010102020101010102020101010102020101010101020201010101010202010102010101020202010102010101020202010202020102020201020202020102020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202010202010202010202010202010202020202010202020202010202020202010202020202020202020202020201020201020201020201020

here is what i want to do

0080040004000100**DATA**010202010202020202020202

all solutions much appritiated.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
LiquidT
  • 1
  • 2

2 Answers2

0

By default, regex quantifiers are greedy; they'll take as much as they possibly can when you give a .* for instance. You can switch to non-greedy mode by appending a ?, making the regex:

r"0080040004000100(.*?)010202010202020202020202"

Note also that I added a leading r to make this a raw string literal. It makes no difference at all here, but it's a good idea to use raw string literals exclusively for regular expressions, as failing to do so will eventually bite you, e.g. when you want a word boundary, r'\b', and search for the ASCII backspace character '\b'.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

Change your regex to:

DTC_data = re.search("0080040004000100(.*?)010202010202020202020202", file)

The ? will make it non-greedy.

zipa
  • 27,316
  • 6
  • 40
  • 58