Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

Regex to search two string inside a method

How to create a regex , so as to search two strings "greet" AND inside this method string "name" . I tried

(^.*greet(
|.|)*)(.*name*)
def greet(name):
    print("Hello, " + name + ". Good morning!") <--- this name should be selected 

def meet(name):
    print("Lets meet, " + name )
question from:https://stackoverflow.com/questions/65848228/regex-to-search-two-string-inside-a-method

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would use this regex:

greet([^
]|
+[^S
])*name

Here the strings greet and name are separated by characters that are not a linebreak ([^ ]) or, in the case, they must be eventually followed by a space that is not a linebreak ([^S ]). In this way you ensure that name is in the same method of greet.

See demo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...