Can I use the logical condition AND in a regex?

Say, if I have a DN string, something like this:

OU=Karen,OU=Office,OU=admin,DC=corp,DC=Fabrikam,DC=COM

How to make a regex to select only DNs that have both OU=Karenand OU=admin?

+5
source share
6 answers

This is a regular expression regular expression matching the entire string if it contains the required parts in any order for reference only. If you do not store the template in some kind of custom variable, I would stick with the nhahtdh solution.

/^(?=.*OU=Karen)(?=.*OU=admin).*$/

^        - line start
(?=      - start zero-width positive lookahead
.*       - anything or nothing
OU=Karen - literal
)        - end zero-width positive lookahead
         - place as many positive or negative look-aheads as required
.*       - the whole line
$        - line end
+10
source

You understand that you don’t have to do everything with one regular expression or even one regular expression.

, , contains() -type , and .

, , ( ) and. .

, - :

,OU=Karen,.*,OU=admin,|,OU=admin,.*,OU=Karen,

, , ( , , Karen7 administrator-lesser ..).

, , - , :

^OU=Karen(,[^,]*)*,OU=admin,|
^OU=Karen(,[^,]*)*,OU=admin$|
,OU=Karen(,[^,]*)*,OU=admin,|
,OU=Karen(,[^,]*)*,OU=admin$|
^OU=admin(,[^,]*)*,OU=Karen,|
^OU=admin(,[^,]*)*,OU=Karen$|
,OU=admin(,[^,]*)*,OU=Karen,|
,OU=admin(,[^,]*)*,OU=Karen$

enouge - ( , - /).

, , , , :

newString = "," + origString.replace (",", ",,") + ","

, :

,OU=Karen,,OU=Office,,OU=admin,,DC=corp,,DC=Fabrikam,,DC=COM,

:

,OU=Karen,.*,OU=admin,|,OU=admin,.*,OU=Karen,

:

  • .
  • .
  • .
  • , Karen2, .

, ( ) - , - :

str = "OU=Karen,OU=Office,OU=admin,DC=corp,DC=Fabrikam,DC=COM"
elems[] = str.splitOn(",")

gotKaren = false
gotAdmin = false
for each elem in elems:
    if elem = "OU=Karen": gotKaren = true
    if elem = "OU=admin": gotAdmin = true

if gotKaren and gotAdmin:
    weaveYourMagicHere()

, , "" , .

, , , : -)

+6

,

/OU=Karen.*?OU=admin|OU=admin.*?OU=Karen/
+3

contains() indexOf() , . .

( ) , , , .

If you want to perform this type of action several times in the same string , and there are many tokens in the string, you can consider parsing the string and storing in some data structure.

+1
source

Not unless you use vi: it has an operator\&

/(OU=Karen.*OU=admin|ou=admin.*OU=Karen)/

It may be close enough, though or similar.

0
source

You can use something like (OU \ = Karen

-1
source

All Articles