regular expression without using negative look behind

Discussion in 'Software' started by PeterK2003, Oct 3, 2011.

  1. PeterK2003

    PeterK2003 Private E-2

    Anyone know how to write this without the negative look behind? VBscript doesn't support it.

    Thanks~Peter

    .*(?<!authentication )failed.*
     
  2. PC-XT

    PC-XT Master Sergeant

  3. PeterK2003

    PeterK2003 Private E-2

    yeah i had found both of those but just could not get them to work but i am not very strong with regular expressions so maybe it was my error.

    Thanks,
    Peter
     
  4. GermanOne

    GermanOne Guest

    I'd like to help but actually I don't understand your requirement (I'm afraid it's because English isn't my first language :-o). What do you mean if you say "without the negative look behind"? Maybe you could post some example strings you want to match.

    Regards
    GermanOne
     
  5. PeterK2003

    PeterK2003 Private E-2

    2264) [10/01/11 15:44:52 GetCertificate](3) Sign on xxx authentication failed for user xxx@pool-xxx.phlapa.fios.verizon.net..

    this statement failed for some reason

    The fist line should not match but the second should. So I want every with the word "failed" unless it is preceded by "authentication".
     
  6. GermanOne

    GermanOne Guest

    Er, sorry but I only see ONE line :confused
    What substring of that line do you want to return by the script?
    What are the Xs in the middle for, a fixed number of letters or an IP address?

    Regards
    GermanOne
     
  7. PeterK2003

    PeterK2003 Private E-2

    line1) 2264) [10/01/11 15:44:52 GetCertificate](3) Sign on xxx authentication failed for user xxx@pool-xxx.phlapa.fios.verizon.net..

    line2) this statement failed for some reason

    i just replaced the username and IP with XXX. It is not relevant to the search anyways.

    I would like the whole line which is why i have the .* b4 and after.
    Thanks,
     
  8. GermanOne

    GermanOne Guest

    Bad news is that (positive and negative) lookbehind is not supported at all.
    I had to take a shower to refresh my gray matter and think about a workaround.
    That's the result:
    Code:
    Const strFile = "c:\somewhere\test.txt"
    Const strPattern1 = "(^|\n).*\bfailed\b.*(\n|$)"
    Const strPattern2 = "\bauthentication failed\b"
    
    'Read the entire file content
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strContent = objFSO.OpenTextFile(strFile).ReadAll
    Set objFSO = Nothing
    
    'create the first RegExp object with pattern 1
    Set objRegEx1 = New RegExp
    objRegEx1.Global = True
    objRegEx1.IgnoreCase = False
    objRegEx1.Pattern = strPattern1
    
    'create the second RegExp object with pattern 2
    Set objRegEx2 = New RegExp
    objRegEx2.Global = False
    objRegEx2.IgnoreCase = False
    objRegEx2.Pattern = strPattern2
    
    'execute the first to get each line containing "failed"
    Set colMatches1 = objRegEx1.Execute(strContent)
    For Each objMatch1 In colMatches1
      strLine = objMatch1.Value
      'test the second if phrase "authentication failed" is NOT part of the line
      If Not objRegEx2.Test(strLine) Then
        WScript.Echo strLine
      End If
    Next
    
    Set objRegEx1 = Nothing
    Set objRegEx2 = Nothing
    
    test.txt contains your lines in this case

    Hope that helps
    GermanOne
     
  9. GermanOne

    GermanOne Guest

    I've just noticed that I made ​​some mistakes.
    Replace the following lines:
    -we have to match the carriage return at the end of the line
    Code:
    Const strPattern1 = "(^|\n).*\bfailed\b.*(\r|$)"
    -we have to remove line feed and carriage return from the returned string
    Code:
      strLine = Replace(Replace(objMatch1.Value, vbCr, ""), vbLf, "")
    Regards
    GermanOne
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds