Question

I need a Regex to find the certain text in document and change and paste in new line.

logging.info("hello i am here")

need to find all occurrence of logging.info and change to

print("hello i am here")

Final looks like this

logging.info("hello i am here")
print("hello i am here")

Is their any regex that i can do or i need to do manually.

Was it helpful?

Solution

I believe a regex like this should work:

^(\h*)logging\.info\(([^)]*)\)

Replace with:

$0\n$1print($2)

regex101 demo

Description:

^                 # Beginning of line
(\h*)             # Get any spaces/tabs before the line and store in $1
logging\.info\(   # Match 'logging.info('
([^)]*)           # Get everything between parens
\)                # Match closing paren

Note that the above regex assumes there are no other parens within the logging.info function.

The replace means:

$0                # Whole match
\n                # Newline
$1                # Place the indentation
print(            # 'print('
$2                # The part within the `logging.info` function
)                 # Closing paren
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top