Java正则表达式问题

Jas*_*zek 1 java regex

我正在研究这段代码,它检查一行动作脚本3代码是否存在一个类型(MovieClip,Sprite,以及类路径中定义的自定义类),这些代码正在被迭代的集合中.

for (String type: typeList) {
    if (input.contains(type)) {
        // dome something here
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是,一些自定义类型名称还包含另一种类型的名称:

Custom type: fSceneController
Contains flash type: Scene
Run Code Online (Sandbox Code Playgroud)

所以.contains方法无法正常工作.我想在循环中使用正则表达式,模式检查类型,并确保在类型之前或之后没有a-zA-Z0-9.

Pattern p = Pattern.compile("<stuff here>"+ type + "<more stuff here>");
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我确定我应该在类型之前和之后放置什么,以便可以从可能包含部分文本的其他类型中清楚地检测到类型本身?

或者也许建议一种我可以用来实现同一目标的不同方法?

Tim*_*han 6

我不清楚你想要做什么,但我认为这是你所缺少的

如果你想要一个正则表达式中的单词而只是单词,那么将\ b放在前面和后面,例如

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

he
she
the
they
Run Code Online (Sandbox Code Playgroud)