Regex except words
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Regex except words

  1. #1
    Join Date
    Oct 2011
    Posts
    2

    Regex except words

    Hello,
    I have a problem with Regex in c# pattern.
    I want to except set of chars(words) from searching in text. This syntax [^abc] i delete each a, b and c from my search but not a whole word "abc". How can I do it to delete a several words?

  2. #2
    Join Date
    Aug 2008
    Posts
    73

    Re: Regex except words

    Hi,

    do not know what exactly you want to achieve.

    But if abc is to match only "abc" then the string pattern should be "^abc$"
    If it has to match abcd then "^abc"

    In both the cases it will not match a, b, c.

    Remove [] from your pattern.

    In order to delete, just replace your pattern with "". use Regex.Replace(,,) method

    Ciao
    Last edited by ricky_cpp; October 14th, 2011 at 04:04 AM.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Regex except words

    I personally wouldn't use a regex for this.

    Try this :

    Code:
    using System;
    using System.Linq;
    
    // ...
    
    string test = "abc hello there abc this abcxx isabc a test abc";
    string x = string.Join(" ", test.Split(' ').Where(i => i != "abc"));
    System.Console.WriteLine(x);
    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Oct 2011
    Posts
    2

    Re: Regex except words

    I have a few string's like this in string's array:

    AT52156123156
    AT5643215231
    5632156AT564
    2315655153AT
    NL648312315
    6513N21L56NL
    34N534AFVNL

    And I want to remove every "AT" and "NL" (and in diffrent case other BE, DE). I want to remove exactly "AT" and "NL" not every A or T or N or L, but a whole set of word. How to use Regex?

    "^abc$" - this pattern won't work in middle of string's.

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Regex except words

    Have you looked at string.Replace method ?

    e.g.

    Code:
    string x = "AT52156123156\r\nNL648312315";
    string result = x.Replace("AT", string.Empty);
    result = result.Replace("NL", string.Empty);
    Just call it multiple times for each string you want removed.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Aug 2008
    Posts
    73

    Re: Regex except words

    Yes Foxc it won't work if you do make effort to read a manual or press F1 for the function help

    $ sign specifies that the match should end at the last chracter of string and specifies that the match must start at the beginning of a string


    Regex.Replace(str, @"(AT)*(NL)*", string.Empty);
    Last edited by ricky_cpp; October 14th, 2011 at 10:09 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center