简体   繁体   English

在Java中找到正确的正则表达式模式

[英]finding right regex pattern in java

So lets say I have array of Strings of following type 所以我要说我有以下类型的字符串数组

something **foo**garblesomething somethingelse
something **foobar**blahblah somethingelse
and so on

So essentially data are triples 所以本质上数据是三倍的

Now, in the middle part of string 现在,在字符串的中间

Notice key word "foo" and "foobar" 注意关键词“foo”和“foobar”

What I want to do is: 我想做的是:

if "foo" in string:
    return 1
else if "foobar" in string:
    return 2
else -1

How do I do this in java? 如何在Java中执行此操作? Thanks 谢谢

Just use String#contains(String) 只需使用String#contains(String)

if (str.contains("foobar"))
    return 2;
else if (str.contains("foo"))
    return 1;
else
    return -1;

Important to check for foobar before foo otherwise it will return 1 for both cases. 重要的是在foo之前检查foobar否则它将在两种情况下返回1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM