Results 1 to 11 of 11

Thread: [RESOLVED] Regular Expression for A NOT B

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [RESOLVED] Regular Expression for A NOT B

    Using regular expressions, how can we find all paragraphs of match type A NOT B

    E.g Find all paragraphs which has the word "pattern" but doesn't have the word "regular".
    So in the following text, 2 & 3 should be selected while 1 should be omitted:
    1. With regular expressions you can describe almost any text pattern
    2. including a pattern that matches two words near each other.
    3. This pattern is relatively simple, consisting of three parts.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Regular Expression for A NOT B

    Hi, have a look at msdn for the right syntax to use : MSDN regular expressions

    I think you could do in in 2 steps. first check if it matches "^((?!regular).)*$" which will return lines that do NOT contain "regular" and then for each match check if it matches something like ".*pattern.*" and you're done !

    have a look at the link I posted to better understand these regular expressions.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Regular Expression for A NOT B

    That requires 2 passes thru the data. I was looking for something that can do this in just one pass as performance is critical to the application.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Regular Expression for A NOT B

    RegEx it's faster than reading and comparing strings in memory?

    If not i think that 's a good way, split all text for line breaks, then for each line if string contains "pattern" and not the "regular" just write them to a new file/array whatever...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  5. #5
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Regular Expression for A NOT B

    Sorry i take a look at some posts in the web, and yes the RegEx it's faster in the most of the cases...

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  6. #6
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Regular Expression for A NOT B

    Quote Originally Posted by Pradeep1210 View Post
    That requires 2 passes thru the data. I was looking for something that can do this in just one pass as performance is critical to the application.
    Then this would be it :

    ^((?!regular).)*pattern((?!regular).)*$
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Regular Expression for A NOT B

    Quote Originally Posted by stlaural View Post
    Then this would be it :

    ^((?!regular).)*pattern((?!regular).)*$
    Very close, but not quite. Should not have the begining and end of string characters in the pattern because that will require matching of the whole input string. The OP wants to match substrings (line) within the input string, so the pattern should be like this:
    Code:
    (?<=\n)((?!regular).)*pattern((?!regular).)*
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Regular Expression for A NOT B

    Quote Originally Posted by stanav View Post
    Very close, but not quite. Should not have the begining and end of string characters in the pattern because that will require matching of the whole input string. The OP wants to match substrings (line) within the input string, so the pattern should be like this:
    Code:
    (?<=\n)((?!regular).)*pattern((?!regular).)*
    Well it does work perfectly with the set of examples that were given. If I use them at once as my input string I get line 2 & 3 as matches. But just to be sure :

    ^ : Matches the position at the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position following '\n' or '\r'.

    $ : Matches the position at the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position preceding '\n' or '\r'.

    So with multiline property enabled its the same thing right ?
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Regular Expression for A NOT B

    Quote Originally Posted by stlaural View Post
    So with multiline property enabled its the same thing right ?
    Yes, that's true. But the default regex options in VS is none, thus unless the OP turns the multiline on, that pattern won't work. On the other hand, if we match a new line character at the begining of the pattern as I did, it will work regardless of what regex multiline option is.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Regular Expression for A NOT B

    Quote Originally Posted by stanav View Post
    Yes, that's true. But the default regex options in VS is none, thus unless the OP turns the multiline on, that pattern won't work. On the other hand, if we match a new line character at the begining of the pattern as I did, it will work regardless of what regex multiline option is.
    Good point ! thanks for the precisions. So Pradeep1210 now has two way to accomplish what he was trying to do.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  11. #11

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Regular Expression for A NOT B

    Thanks for the great help
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width