Regular Expressions: Is there an AND operator?

后端 未结 12 2004
刺人心
刺人心 2020-11-21 06:34

Obviously, you can use the | (pipe?) to represent OR, but is there a way to represent AND as well?

Specifically, I\'d like to

相关标签:
12条回答
  • 2020-11-21 06:57

    You can do that with a regular expression but probably you'll want to some else. For example use several regexp and combine them in a if clause.

    You can enumerate all possible permutations with a standard regexp, like this (matches a, b and c in any order):

    (abc)|(bca)|(acb)|(bac)|(cab)|(cba)
    

    However, this makes a very long and probably inefficient regexp, if you have more than couple terms.

    If you are using some extended regexp version, like Perl's or Java's, they have better ways to do this. Other answers have suggested using positive lookahead operation.

    0 讨论(0)
  • 2020-11-21 07:00

    Why not use awk?
    with awk regex AND, OR matters is so simple

    awk '/WORD1/ && /WORD2/ && /WORD3/' myfile
    
    0 讨论(0)
  • 2020-11-21 07:01

    In addition to the accepted answer

    I will provide you with some practical examples that will get things more clear to some of You. For example lets say we have those three lines of text:

    [12/Oct/2015:00:37:29 +0200] // only this + will get selected
    [12/Oct/2015:00:37:x9 +0200]
    [12/Oct/2015:00:37:29 +020x]
    

    See demo here DEMO

    What we want to do here is to select the + sign but only if it's after two numbers with a space and if it's before four numbers. Those are the only constraints. We would use this regular expression to achieve it:

    '~(?<=\d{2} )\+(?=\d{4})~g'
    

    Note if you separate the expression it will give you different results.

    Or perhaps you want to select some text between tags... but not the tags! Then you could use:

    '~(?<=<p>).*?(?=<\/p>)~g'
    

    for this text:

    <p>Hello !</p> <p>I wont select tags! Only text with in</p> 
    

    See demo here DEMO

    0 讨论(0)
  • 2020-11-21 07:05

    If you use Perl regular expressions, you can use positive lookahead:

    For example

    (?=[1-9][0-9]{2})[0-9]*[05]\b
    

    would be numbers greater than 100 and divisible by 5

    0 讨论(0)
  • 2020-11-21 07:11

    You need to use lookahead as some of the other responders have said, but the lookahead has to account for other characters between its target word and the current match position. For example:

    (?=.*word1)(?=.*word2)(?=.*word3)
    

    The .* in the first lookahead lets it match however many characters it needs to before it gets to "word1". Then the match position is reset and the second lookahead seeks out "word2". Reset again, and the final part matches "word3"; since it's the last word you're checking for, it isn't necessary that it be in a lookahead, but it doesn't hurt.

    In order to match a whole paragraph, you need to anchor the regex at both ends and add a final .* to consume the remaining characters. Using Perl-style notation, that would be:

    /^(?=.*word1)(?=.*word2)(?=.*word3).*$/m
    

    The 'm' modifier is for multline mode; it lets the ^ and $ match at paragraph boundaries ("line boundaries" in regex-speak). It's essential in this case that you not use the 's' modifier, which lets the dot metacharacter match newlines as well as all other characters.

    Finally, you want to make sure you're matching whole words and not just fragments of longer words, so you need to add word boundaries:

    /^(?=.*\bword1\b)(?=.*\bword2\b)(?=.*\bword3\b).*$/m
    
    0 讨论(0)
  • 2020-11-21 07:12

    The AND operator is implicit in the RegExp syntax.
    The OR operator has instead to be specified with a pipe.
    The following RegExp:

    var re = /ab/;
    

    means the letter a AND the letter b.
    It also works with groups:

    var re = /(co)(de)/;
    

    it means the group co AND the group de.
    Replacing the (implicit) AND with an OR would require the following lines:

    var re = /a|b/;
    var re = /(co)|(de)/;
    
    0 讨论(0)
提交回复
热议问题