【问题标题】:regex for matching string with multiple occurance in java用于在java中匹配多次出现的字符串的正则表达式
【发布时间】:2014-05-15 07:29:25
【问题描述】:

我需要一个正则表达式,它匹配下面的 java 字符串。

select * from test

select * from test where a=100

select * from test where a=100 and b=100

select 语句可能包含也可能不包含 where 条件,也可能包含 1 到 n 个条件,我需要一个正则表达式来匹配所有这些条件,谁能帮忙

我尝试使用下面的正则表达式匹配字符串,但失败了 select (([A-Za-z0-9]{1,20}(|\\,))*|\\*) from ([A-Za-z0-9]{1,20})( where ([A-Za-z0-9]{1,20}((| )(\\=|\\>|\\<)(| ))[A-Za-z0-9]{1,20})|$)

标准:

i) 字符串应以 select

开头

ii) 如果 where 存在,则后面应该有一对变量和值

  like below:

        `select * from test where a=10;`

iii) 在两个变量和值对之间 and 应该存在

  like below:
         `select * from test where a=10 and b=10`  

iv) 字符串可能包含也可能不包含 where

v) 字符串不应以 and

结尾
public class Test {
public static Pattern pattern;
public static Matcher matcher;

private static final String PATTERN="(\\s)*select(\\s)+((\\*)|([a-zA-Z0-9]+))(\\s)+from(\\s)+[a-zA-Z0-9]*(\\s)+where(\\s)+(‌​[a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*)+(\\s)*((\\s)*and(\\s)*([a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*)‌​)*(\\s)*";


public static void main(String[] args)throws IOException {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    String data;        
    pattern=Pattern.compile(PATTERN,Pattern.CASE_INSENSITIVE);
    do{         
    data=br.readLine();
    matcher=pattern.matcher(data);
    if(matcher.find()){
        System.out.print("True");
    }else{
        System.out.print("false");
    }
    System.out.print(":"+data+":\n");
    }while(data!="exit");
}

}

测试用例(应为真)

从测试中选择一个

从 a=10 的测试中选择一个

从 a=10 和 b=10 的测试中选择一个

从 a=10 and b=10 and c=10 的测试中选择 a

测试用例(应显示为 false)

从测试中选择一个

从测试中选择 a=10 并且

从 a=10 b=10 的测试中选择一个

从 a=10 和 b=10 的测试中选择一个并且

谢谢

【问题讨论】:

  • 你应该开始做这件事并提交你的进度:这样我们就知道如何衡量答案的细节了。
  • @Pradeep 我会把它放到你的问题中
  • 请将您的正则表达式放在实际问题中:它实际上不属于评论;你会像现在这样吸引反对票。
  • 所以你只想匹配以select * from test开头的每一行,或者你想解析条件还是......?你能给出预期的输出吗?
  • 它类似于 sql select 语句,可能因情况而异。下面是示例字符串 select a from test select a,b from group select * from group where a=1 and b=10 etc

标签: java regex


【解决方案1】:

我是正则表达式的新手。阅读此问题后,我在在线正则表达式测试器中尝试了以下内容。它工作正常。

(\s)*select(\s)+((\*)|([a-zA-Z0-9]+))(\s)+from(\s)+[a-zA-Z0-9]*(\s)+where(\s)+([a-zA-Z0-9]*(\s)*=(\s)*[0-9]*)+((\s)+and(\s)+([a-zA-Z0-9]*(\s)*=(\s)*[0-9]*))*(\s)*

我觉得太长了。但它适用于案例 2 和案例 3。如果您只想通过 regex 实现这些案例,则需要两个 regex 来实现您想要的解决方案。

如果您想在单个正则表达式中实现所有三个,这是不可能的[据我所知]。
As Bobbel mentioned in comments,这是不可能的。

因为你需要检查“where”是否存在。如果存在,它应该有条件。其他“位置”本身不应该出现。

编辑

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld
{
  private static final String PATTERN="(\\s)*select(\\s)+((\\*)|([a-zA-Z0-9]+))   (\\s)+from(\\s)+[a-zA-Z0-9]*(\\s)+where(\\s)+([a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*)+ ((\\s)+and(\\s)+([a-zA-Z0-9]*(\\s)*=(\\s)*[0-9]*))*(\\s)*";

 public static void main(String []args)
 {
    System.out.println("Hello World");
    String data="select * from asdf where a  = 20 and ax =20";        
    Pattern pattern=Pattern.compile(PATTERN); 
    Matcher matcher=pattern.matcher(data);
    if(matcher.find())
    {
      System.out.print("True");
    }
    else
    {
      System.out.print("false");
    }
    System.out.print(":"+data+":\n");
 }

}

【讨论】:

  • 也可以将所有三合一匹配为:select...([\s]+where...([\s]+and...)*)* - regex101.com/r/uY2iP0。但无论如何,你对这些表达方式非常严格。 IS NULL<>>=<= 呢?我仍然不知道,@Pradeep 期望什么......
  • 是的,它仍然是可能的。这将是太大的正则表达式。他只指定了 "and" 。所以我不检查关系运算符。
  • 这就是我在上面那个正则表达式中给出的。即使您添加 100 个“和”,它也有效。
  • @user3168736:您提供的正则表达式不符合我在问题中提到的标准。
  • @Pradeep 试试下面的(\s)*select(\s)+((\*)|([a-zA-Z0-9]+))(\s)+from(\s)+[a-zA-Z0-9]*(\s)+where(\s)+([a-zA-Z0-9]*(\s)*=(\s)*[0-9]*)+(\s)*((\s)*and(\s)*([a-zA-Z0-9]*(\s)*=(\s)*[0-9]*))*(\s)* 你能否发布你的测试用例示例。
【解决方案2】:

我终于找到了我的问题的解决方案。这将符合我的所有标准。

select\s(([A-Za-z0-9]{1,20})(|\,[A-Za-z0-9]{1,20})+|\*)\sfrom\s([A-Za-z0-9]{1,20})(\swhere\s[A-Za-z0-9]{1,20}\=[A-Za-z0-9]{1,20}($|(\sand\s[A-Za-z0-9]{1,20}\=[A-Za-z0-9]{1,20})+$)|$)

希望以后能对大家有所帮助。

【讨论】:

    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2011-06-21
    相关资源
    最近更新 更多