Contains the word but not a specific word before it

非 Y 不嫁゛ 提交于 2021-01-27 20:38:23

问题


I need help in regular expressions. The code is in SQL and I would like to match the all string that is not referencing proxy database.

The pattern would be: Contains .. and the word before it is not equal to proxy

I would like to match the example word customer below:

from proxy..vw_xxx a join customer..vw_xxx b
from proxy..vw_xxx
insert into proxy..vw_xxx
from customer..vw_xxx

Edit: How about if I would also like to capture the DDL statement. For Example: CREATE VIEW vw_yyy AS ... from proxy..vw_xxx a join customer..vw_xxx b from proxy..vw_xxx insert into proxy..vw_xxx from customer..vw_xxx GO CREATE VIEW vw_zzz AS SELECT * FROM customer.vw_xxx GO

I would just like to capture the first CREATE VIEW. I used the regex:

CREATE VIEW.*\w+(?<!proxy)...*GO

and /gmis option but it's also matching the second creation of view.

Would appreciate any help. Thanks.


回答1:


Use a negative lookbehind based regex.

java regex

"\\w+(?<!proxy)\\.\\."

C# regex

@"\w+(?<!proxy)\.\."

\w+ matches one or more word characters and following double dots only if the dots are not preceded by the word proxy.

DEMO

String s = "from proxy..vw_xxx a join customer..vw_xxx b\n" + 
        "from proxy..vw_xxx\n" + 
        "insert into proxy..vw_xxx\n" + 
        "from customer..vw_xxx";
Matcher m = Pattern.compile("\\w+(?<!proxy)\\.\\.").matcher(s);
while(m.find())
{
    System.out.println(m.group());
}

Output:

customer..
customer..


来源:https://stackoverflow.com/questions/28271443/contains-the-word-but-not-a-specific-word-before-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!