Tech Off Thread

11 posts

Regular Expression to remove an attribute

Back to Forum: Tech Off
  • User profile image
    Harlequin

     x:Uid="XXXXX"

     

    I want to remove all these tags from a Xaml file. Is there a regex I can use to take them all out? XXXXX can be anything. And note a space before the x.

  • User profile image
    ktr

    Regex:

    \s+x:Uid=\"[^\"]+\"

     

    in C#:

    var regex = "\\s+x:Uid=\\\"[^\\\"]+\\\"";

    ... try that

     

    Edit: Updated regex.

  • User profile image
    Harlequin

    ktr said:

    Regex:

    \s+x:Uid=\"[^\"]+\"

     

    in C#:

    var regex = "\\s+x:Uid=\\\"[^\\\"]+\\\"";

    ... try that

     

    Edit: Updated regex.

    Unknown operator on the :

     

    Might need an escape or something?

  • User profile image
    ktr

    Harlequin said:
    ktr said:
    *snip*

    Unknown operator on the :

     

    Might need an escape or something?

    Hmm.. that is strange.  That is definitely the right expression.  It works with C#'s regex engine and Javascript's engine too.  I tried using it in VS2008's find dialog and I got the same error.  I don't know how to fix that.  I think they have some custom searches you can perform that are prefixed with a colon.  That kind of botches the regex functionality...

  • User profile image
    ktr

    ktr said:
    Harlequin said:
    *snip*

    Hmm.. that is strange.  That is definitely the right expression.  It works with C#'s regex engine and Javascript's engine too.  I tried using it in VS2008's find dialog and I got the same error.  I don't know how to fix that.  I think they have some custom searches you can perform that are prefixed with a colon.  That kind of botches the regex functionality...

    Ok.. i finally figured it out... VS has a different syntax for finding whitespace (:Wh).  Anyway, here it is (this will only work in the find dialog):

     

    :Whx\:Uid="([^"]|:Wh)*"

  • User profile image
    Sven Groot

    ktr said:
    Harlequin said:
    *snip*

    Hmm.. that is strange.  That is definitely the right expression.  It works with C#'s regex engine and Javascript's engine too.  I tried using it in VS2008's find dialog and I got the same error.  I don't know how to fix that.  I think they have some custom searches you can perform that are prefixed with a colon.  That kind of botches the regex functionality...

    Visual Studio's find dialog uses a different regex syntax. This ought to do the trick:

     

    :b+x\:Uid="[^"]+"

  • User profile image
    ktr

    Sven Groot said:
    ktr said:
    *snip*

    Visual Studio's find dialog uses a different regex syntax. This ought to do the trick:

     

    :b+x\:Uid="[^"]+"

    Not when there are newlines between the quotes.

  • User profile image
    Sven Groot

    ktr said:
    Sven Groot said:
    *snip*

    Not when there are newlines between the quotes.

    If there are newlines between the quotes, then it isn't a valid XML file. Smiley

  • User profile image
    Harlequin

    Sven Groot said:
    ktr said:
    *snip*

    Visual Studio's find dialog uses a different regex syntax. This ought to do the trick:

     

    :b+x\:Uid="[^"]+"

    Gimme 3 weeks to try it out...on vacation now Smiley

     

    Cheers

  • User profile image
    Ion Todirel

    Harlequin said:
    Sven Groot said:
    *snip*

    Gimme 3 weeks to try it out...on vacation now Smiley

     

    Cheers

    what kind of vacation is that without a laptop with Visual Studio connected to the internet? Big Smile

  • User profile image
    Harlequin

    Ion Todirel said:
    Harlequin said:
    *snip*

    what kind of vacation is that without a laptop with Visual Studio connected to the internet? Big Smile

    :b+x\:Uid="[^"]+" worked awesome, thanks a bunch.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.