问题描述:

I am working on this bit of code that checks a line of actionscript 3 code for the existance of a type (MovieClip, Sprite, along with the custom classes defined in the classpath) that is in a collection that is being iterated.

for (String type: typeList) {

if (input.contains(type)) {

// dome something here

}

}

The problem is, some of the custom type names also contain the name of another type:

Custom type: fSceneController

Contains flash type: Scene

So the .contains method will not work properly. I was thinking of using a regex inside the loop where the pattern checks for the type and makes sure that there are no a-zA-Z0-9 immediately before or after the type.

Pattern p = Pattern.compile("<stuff here>"+ type + "<more stuff here>");

Can anyone help me determine what i should put before and after the type so that the type itself can be detected distinctly from other types that may contain part of the text?

Or perhaps suggest a different method that i can use to accomplish the same goal?

网友答案:

Not sure I'm clear on what you're trying to do, but I think this is what you're missing

If you want a word in a regex and just the word, then put \b in front and in back, for instance

\bhe\b will only match the first of...

he
she
the
they
网友答案:

You might be able to check the length as well, like this

for(String type : typeList)
    if(input.contains(type) && input.length() == type.length())
        System.out.println("Found " + type);
相关阅读:
Top