简体   繁体   English

Java正则表达式单词不以点结尾

[英]java regular expression word without ending with dot

I need to print the simple bind variable names in the SQL query. 我需要在SQL查询中打印简单的绑定变量名称。 I need to print the words starting with : character But NOT ending with dot . 我需要打印以:字符开头的字符: character但不以dot结尾. character. 字符。 in this sample I need to print pOrg , pBusinessId but NOT the parameter . 在此示例中,我需要打印pOrgpBusinessId而不是parameter The regular expression ="(:)(\\\\w+)^\\\\." 正则表达式="(:)(\\\\w+)^\\\\." is not working. 不管用。

Could you help in correcting the regular expression. 您能帮忙纠正正则表达式吗?

Thanks Peddi 谢谢佩迪

public void testMethod(){ 
String regEx="(:)(\\w+)([^\\.])";  
String input= "(origin_table like 'I%' or (origin_table like 'S%' and        process_status =5))and header_id = NVL( :parameter.number1:NULL, header_id) and (orginization = :pOrg) and (businsess_unit = :pBusinessId";
Pattern pattern;
Matcher matcher;

    pattern = Pattern.compile(regEx);
    matcher = pattern.matcher(input);

    String grp = null;

    while(matcher.find()){

        grp = matcher.group(2);
        System.out.println(grp);

    }
}

You can try with something like 您可以尝试类似

String regEx = "(:)(\\w+)\\b(?![.])";
  • (:)(\\\\w+)\\\\b will make sure that you are matching only entire words starting with : (:)(\\\\w+)\\\\b将确保您只匹配以:开头的整个单词
  • (?![.]) is look behind mechanism which makes sure that after found word there is no . (?![.])机制的后盾 ,该机制确保找到的单词之后没有.

This regex will also allow :NULL so if there is some reason why it shouldn't be matched share it with us. 此正则表达式还将允许:NULL因此,如果出于某些原因不匹配它,请与我们分享。


Anyway to exclude NULL from results you can use 无论如何要从结果中排除NULL

String regEx = "(:)(\\w+)\\b(?![.])(?<!:NULL)";

To make regex case insensitive so NULL could also match null compile this pattern with Pattern.CASE_INSENSITIVE flag like 为了使正则表达式不区分大小写,因此NULL也可以与null匹配,使用Pattern.CASE_INSENSITIVE标志编译该模式,例如

Pattern pattern = Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);

You can try: 你可以试试:

Pattern regex = Pattern.compile("^:.*?[^.]$");

正则表达式可视化

Demo 演示

Since it looks like you're using camelcase, you can actually simplify things a bit when it comes to excluding :NULL : 由于看起来好像您在使用驼峰箱,因此在排除:NULL时,实际上可以简化一些事情:

:([az][\\\\w]+)\\\\b(?!\\\\.)

And $1 will return your variable names. $1将返回您的变量名。

Alternative that doesn't rely on negative lookahead: 不依赖否定前瞻的替代方法:

:([az][\\\\w]+)\\\\b(?:[^\\\\.]|$)

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

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