2

I have a large string full of byte code so there is no spaces, I am trying to search for a specific part of that code.

For example;

byte_code = 'ergewyegt6t4738t8347tygfhfh47tg4378t58437t485t454gyfofby3gyfg34t5t6435t3465tg3465t4'

The value I am looking for is 'fhfh47tg'.

I have tried the .find String method but did not have much luck. As mentioned before, the string has no spaces.

I'd expect the output to be true once it has found the pattern within the string.

  • `'fhfh47tg' in byte_code` ? – RomanPerekhrest Jul 19 '19 at 09:35
  • 1
    Please include the code you have tried. str.find should work fine, but it returns `-1` if there's no match. If you don't care where in the string your pattern is found, use `in` operator instead. – Håken Lid Jul 19 '19 at 09:37
  • @AniketSahrawat: That will return False only if the substring is found at the beginning of the search string (index 0, which is a falsy value). A non-match will return `-1` which is truthy. Instead you could use `print(byte_code.find('fhfh47tg') != -1)` – Håken Lid Jul 19 '19 at 09:47
  • @HåkenLid Didn't thought about that :) – Aniket Sahrawat Jul 19 '19 at 09:48
  • 1
    Possible duplicate of [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – ruohola Jul 19 '19 at 10:05

4 Answers4

5

You can use the in (__contains__) test for substring existence:

if 'fhfh47tg' in byte_code:
    print('Match found')

You can also look at methods like str.find, str.index FWIW.

heemayl
  • 39,294
  • 7
  • 70
  • 76
2

Using re.search is one option:

if re.search(r'fhfh47tg', byte_code):
    print("MATCH")
else:
    print("NO MATCH")
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

I think you can do

if 'fhfh47tg' in byte_code:
    return True

or

return 'fhfh47tg' in byte_code
0

you can try the following:

byte_code = 'ergewyegt6t4738t8347tygfhfh47tg4378t58437t485t454gyfofby3gyfg34t5t6435t3465tg3465t4'
    
if 'fhfh47tg' in byte_code:
    print("Value is present.")

(you can change the output as per your liking.)

Peace out.

Peter Westlake
  • 4,894
  • 1
  • 26
  • 35
Parthik B.
  • 472
  • 1
  • 5
  • 15