如何匹配泛型string =泛型字符串

时间:2018-02-07 09:53:04

标签: regex grep string-parsing

假设我有这个文件:

=

我想匹配=左边的行与AND 1=1 AND fejo = fejo 符号右边的行匹配,因此,它必须与以下行匹配:

if(uniqueId == null) { 
    uniqueId = UUID.randomUUID().toString(); 
}

3 个答案:

答案 0 :(得分:2)

grep -E '^AND\s+([^=\s]*)\s*=\s*\1\b'

可以正常输入。

正则表达式

^               # begin of line (preg tries to match the regex against each line)
AND             # match literal 'AND'
\s+             # match one or more whitespace characters
(               # beginning of a group
    [           # beginning of a character class that...
        ^       #    ... match any character that is not listed here:
        =       #    literal '='
        \s      #    whitespace
    ]           # end of the character class...
                # ... that matches one character that is not '=' or whitespace
    *           # zero or more occurrences of the previous expression (the class)
)               # end of the capturing group
\s*             # match zero or more spaces... 
=               # the '=' character
\s*             # ... around the equal sign
\1              # match the text captured by the first (and only) group above
\b              # match a word boundary, to make sure \1 is not just a prefix of a longer word

上面的regex仅匹配以大写AND开头的行。如果您还需要匹配以and(小写)开头的行或这些字符的其他大写/小写组合,则可以使用AND替换regex中的[aA][nN][dD]

-i添加到grep命令行会使其忽略regex和输入中的大小写。 regex将匹配and 1 = 1,但也会and fejo = FEJO,这可能不是您所需要的。

答案 1 :(得分:0)

使用awk:

$ awk 'split($0,a," *= *") && a[1]==($1 " " a[2])' file
AND 1=1
AND fejo = fejo

split=上的记录拆分为AND 11,在$1前面添加ANDa[2] } {ie 1并进行比较。如果在$1之后有多个空格,则会失败。为避免这种情况,这似乎也有效:

$ awk 'split($0,a,"( *= *| *)") && a[2]==a[3]' file
AND 1=1
AND fejo = fejo

缺点是被比较的元素不能有空间。这个清除了第一个单词及其周围空间śplit s =(包括周围空间)并比较了半个。

$ awk ' {
    r=$0                     # working copy of record
    sub(/^ *[^ ]* */,"",r)   # remove AND
    n=split(r,a," *= *")     # split at = 
    if((n>1)&&a[1]==a[n])    # if r was really split in half and halfs match
        print
}' file
AND 1=1
AND fejo = fejo

答案 2 :(得分:0)

我找到了另一个非常简单的解决方案,而不必弄得一团糟:

AND (\w+)\s*=\s*\1