Python Forum
How to split a string containing function calls?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split a string containing function calls?
#5
I believe I have found the answer to my problem: regular expressions.
Remember I said if there was a way to look for all expressions in form add/multiply(int,int) and replace with value? I found out that regular expressions can do that first part.

Here is a simple example of the code I'm using:
import re

pattern = re.compile(r'(add|multiply)\([0-9]+,[0-9]+\)')

s = multiply(add(1,2), add(3,4))

match = pattern.search(s)

print(match.group(0))
This will print out "add(1,2)" which I can then pass to my evaluation function to solve.
I can then replace "add(1,2)" with its value by using string.replace
#My evaluation function takes in a string of the form add(int,int) and returns the result in string form
s = s.replace(match.group(0), add(match.group(0))
after this s will be "multiply(3, add(3,4))

So I can just put this in a loop and it will eventually get me the answer!

Thank you viewers for the moral support and thank you buran for your earlier reply.

Here is the link to the regular expressions tutorial I watched in case someone else needs it: https://www.youtube.com/watch?v=K8L6KVGG-7o
Reply


Messages In This Thread
RE: How to split a string containing function calls? - by Metalman488 - Oct-27-2018, 06:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,487 Aug-13-2023, 01:57 AM
Last Post: Skaperen
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,122 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  [split] Parse Nested JSON String in Python mmm07 4 1,522 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Split string using variable found in a list japo85 2 1,296 Jul-11-2022, 08:52 AM
Last Post: japo85
  Convert a string to a function mikepy 8 2,505 May-13-2022, 07:28 PM
Last Post: mikepy
  asyncio calls within sync function ( Websocket on_open) orion67 0 1,403 Jan-16-2022, 11:00 AM
Last Post: orion67
  Split string knob 2 1,868 Nov-19-2021, 10:27 AM
Last Post: ghoul
  Calls to Attributes of a Class SKarimi 3 3,380 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  list call problem in generator function using iteration and recursive calls postta 1 1,901 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  [split] Creating a variable as a function DPaul 23 6,669 Sep-07-2020, 05:20 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

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