问题
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