2

I have a string:

string s = "GameObject.Find(\"MyObj\").GetComponent(\"MyComponent\")";

I want to extract "GameObject.Find(\"MyObj\")" where MyObj can include any number or type of characters except newline.

This is my code:

Match match = Regex.Match(s, "GameObject.Find(\".+\")");

I know I'm doing something wrong, but I'm not sure where to go from here. How can we make this expression work as intended?

2 Answers 2

3
Match match = Regex.Match(s, "GameObject.Find(\".+?\")");

You should do non-greedy search, but beware that it will only match from parantheses+quotation mark to first quotation mark+parantheses.

So for,

string s = "GameObject.Find(\"seckin(\\\"hand\\\").thumb()\").GetComponent(\"MyComponent\")"

it will match "GameObject.Find(\"seckin(\\\"hand\\\")"

But there is no way to match enclosing parenthesis using RegExp, so it is the best sub-optimal solution.

Sign up to request clarification or add additional context in comments.

Comments

3

Maybe you should try :

Match match = Regex.Match(s, "GameObject.Find(\".+?\")");

2 Comments

I'm faster than you, my friend
Agree with you...My typing is very slow!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.