-2

I need a regex to capture/get number of input functions used in a python code.

I tried this: /input\((.)*\)/gm

But I don't want it to capture something like this:

someVariable = "input( )"


The regex I want should be able to do something like this:

Code:

number = int(input("Enter a number: "))
someVariable = "input( )"
number2 = int(input ("Number: ") )

The output should be 2 matches (input function in line 1 and 3). It should not match the input() in line 2.


P.S: I am new to regex, so it would be nice if you could also write an explanation for your regex solution.

  • What have tried so far? I don't see the regex (`/input((.)*)/gm`) you are talking about in your code. – Austin Nov 03 '19 at 07:34
  • How *"The output should be 2"*? What do you mean by *"It should not count.."*? – Austin Nov 03 '19 at 07:35
  • That code is just an example. I need to run the regex on the python code (As a string). – Srijan Mukherjee Nov 03 '19 at 07:36
  • regexr.com/4o2ms Check this out for clarification. – Srijan Mukherjee Nov 03 '19 at 07:36
  • Re: "What do you mean by "It should not count.."?" I mean the regex should not match the input( ) in "input ( )" since its not a function, just a string (LINE 2) – Srijan Mukherjee Nov 03 '19 at 07:41
  • Don't use regex to [parse structured data](https://stackoverflow.com/a/1732454/7553525), that's not what regex is for. What you need is a structure-aware parser for the structured data you're trying to parse, like the standard library modules [`parser`](https://docs.python.org/3/library/parser.html) and [`ast`](https://docs.python.org/3/library/ast.html). – zwer Nov 03 '19 at 07:44

1 Answers1

2

I would not recommend using regular expressions to solve this problem for roughly the same reasons that you should not use regular expressions to parse XML. Whatever regular expression that you come up with will likely not capture 100% of cases anyway and will end up being overly complex and unmaintainable.

A much more reliable way of counting the number of calls to a particular function in Python is to convert the Python file into an abstract syntax tree via Python's built-in ast module. From there you should be able to traverse the AST to extract your answer.

Example

Given the target Python file (test.py):

a = input("Hello: ")
b = "input()"
c = input("World: ")

In a separate Python file:

import ast
file = open('test.py', 'r')
tree = ast.parse(file.read())

function1 = tree.body[0].value.func  # the function that was called on line 1
print(function1.id == "input")  # true
mario_sunny
  • 1,412
  • 11
  • 29