Python Forum
ValueError: substring not found
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: substring not found
#4
Regex could be a solution:
import re


def on_off_finder(in_string):
    values = ("off", "on")
    # index   [ 0 ]  [ 1]
    # bool(0) -> False
    # bool(1) -> True
    if match := re.search(r"ch=(on|off)", in_string.lower()):
        # it does only match, if ch=on or ch=off was
        # in_string.lower()
        # then getting the group1, which is in parentheses
        # then searching the index for off/on
        # converting the index (0 or 1) into bool
        # return this value
        return bool(values.index(match.group(1)))
    else:
        # ch=on or ch=off was not in_string.lower()
        return False
To check the regex, visit: https://regex101.com/
Don't forget to select on the left side Python

Joke: If you have a problem and want to solve it with regex, then you have two problems.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
ValueError: substring not found - by nby2001 - Mar-08-2020, 06:10 PM
RE: ValueError: substring not found - by deanhystad - Mar-08-2020, 09:00 PM
RE: ValueError: substring not found - by malcomjarr - Aug-08-2022, 05:04 AM
RE: ValueError: substring not found - by DeaD_EyE - Aug-08-2022, 10:05 AM
RE: ValueError: substring not found - by rob101 - Aug-08-2022, 11:16 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 584 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,123 Apr-08-2023, 06:36 PM
Last Post: Winfried
  Match substring using regex Pavel_47 6 1,488 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,504 Jun-16-2022, 06:59 PM
Last Post: saoko
  Substring Counting shelbyahn 4 6,187 Jan-13-2022, 10:08 AM
Last Post: krisputas
  Python Substring muzikman 4 2,359 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,582 Nov-23-2020, 05:15 PM
Last Post: cananb
  Removing items from list if containing a substring pythonnewbie138 2 2,236 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Substring and If then Condition to create column Chandan 2 2,373 Jan-23-2020, 08:40 AM
Last Post: buran
  ValueError: substring not found hoangthai10788 2 4,631 Sep-23-2019, 05:34 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