简体   繁体   English

需要一个正则表达式和示例Java代码来从字符串中提取比较运算符[保留]

[英]Need a regex and sample java code to extract comparative operator from a String [on hold]

I found a string - "Process Execution Time"==">300ms" 我找到了一个字符串-“ Process Execution Time” ==“> 300ms”

Previously i was using a regex to get the operator from other string. 以前,我使用正则表达式从其他字符串获取运算符。 but when the operator is present in value section my existing regex does not work 但是当运算符出现在值部分时,我现有的正则表达式不起作用

My Existing regex - (. )(==|!=|>=|<=|>|<)(. ) 我现有的正则表达式-(。 )(== |!= |> = | <= |> | <)(。

Example : 范例:

"A">"B" Group 1 = A Group 2 = > Group 3 = B “ A”>“ B”组1 = A组2 =>组3 = B

"A"=="B" Group 1 = "A" Group 2 = == Group 3 = "B" “ A” ==“ B”组1 =“ A”组2 = ==组3 =“ B”

it breaks when > is in value section. 当>在值部分时,它会中断。 please note i have all the value with in "". 请注意,我具有“”中的所有值。

"Process Execution Time"==">300ms" “进程执行时间” ==“> 300ms”

Group 1 = "Process Execution Time"==" Group 2 = > Group 3 = 300ms 组1 =“进程执行时间” ==“组2 =>组3 = 300ms

Whether my expectation is - 我的期望是-

Group 1 = "Process Execution Time" Group 2 = == Group 3 = ">300ms" 组1 =“流程执行时间”组2 = ==组3 =“> 300毫秒”

I have tried ungreedy and it worked in www.regex101.com with regex (. )(==|!=|>=|<=|>|<)(. +) 我尝试过不贪心,并且它在www.regex101.com中使用了regex(。 )(== |!= |> = | <= |> | <)(。 +)

But not able get any sample in java fro ungreedy regex matching ... 但是无法在Java中从不匹配正则表达式匹配中获取任何示例...

Thanks in advance for any help .. 预先感谢您的任何帮助..

Regards - Tutai 问候-Tutai

Since you've stated: 既然您已经说过:

please note i have all the value with in "". 请注意,我具有“”中的所有值。

This would make it slightly easier, by just checking those in the regex: 通过仅检查正则表达式中的内容,这将使其变得稍微容易一些:

"([^"]+)"(==|!=|>=|<=|>|<)"([^"]+)"

The [^"]+ means 1 or more non- " characters . [^"]+表示1个或多个非"字符

Here a test program for your three test cases: 这里是针对您的三个测试用例的测试程序:

import java.util.regex.*;
class Main{
  public static void main(String[] a){
    for(String testCase : new String[]{ "\"Process Execution Time\"==\">300ms\"",
                                         "\"A\">\"B\"",
                                         "\"A\"==\"B\"" }){
      System.out.println("Input: "+testCase);
      Matcher matcher = Pattern.compile("\"([^\"]+)\"(==|!=|>=|<=|>|<)\"([^\"]+)\"")
                               .matcher(testCase);
      if(matcher.find()){
        System.out.println("Group 1: "+matcher.group(1));
        System.out.println("Group 2: "+matcher.group(2));
        System.out.println("Group 3: "+matcher.group(3));
      }
      System.out.println();
    }
  }
}

Which will output: 将输出:

Input: "Process Execution Time"==">300ms"
Group 1: Process Execution Time
Group 2: ==
Group 3: >300ms

Input: "A">"B"
Group 1: A
Group 2: >
Group 3: B

Input: "A"=="B"
Group 1: A
Group 2: ==
Group 3: B

Try it online. 在线尝试。

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

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