1

I'm trying to parse the class name:

public class Script {


}

And I'm using regex at the moment but it doesn't work as good as I though, my regex

class\s+(.*?)\W

But when things like extends and implemented comes in it's breaking itself.

I'm making a script that downloads sources from pastebin and auto saves it.

Thanks.

2
  • Try to be stricter about the characters a name can contain. For example it can't contain any whitespace characters. Commented Jun 6, 2012 at 8:09
  • 1
    Doesn't reflection only work with compiled files? Commented Jun 6, 2012 at 8:14

2 Answers 2

2

This could do the trick :

serach for all characters excepts spaces or bracket class\s+([^\s{]+)\s*

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

2 Comments

The Problem with class\s+([^\s]+)\s is, that it would return 'ClassName{' as result, if a space is missing between the ClassName and '{' - example: 'class ClassName{'
you are right, i updated the regexp to stop if there is a space or a "{"
1

Try to use class\s+(\w*)\s*

This would also handle the case of class Classname{ whitout a space after the Classname

Comments

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.