Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. VS2017 : selecting files, then applying search/replace ?

VS2017 : selecting files, then applying search/replace ?

Scheduled Pinned Locked Moved C#
visual-studioregexperlcomtutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BillWoodruff
    wrote on last edited by
    #1

    I have been struggling with what Jeff Atwood calls VS's "bastardized regular expression syntax" [^]. Along the way, I found Peter Macej's very useful VS extension for multiple-line-search-replace [^]. It kind of warps my mind that VS, at this late date, doesn't have a built-in multi-line search and replace, and doesn't support a more "standard" RegEx syntax ! Using 'Find in Files' in VS it's easy to limit the search to specific types of files, like *.cs. Where I am stumped is how to select files that have specific text (keyword) content, and then apply S&R to only the matching files.

    // file1.cs
    // ... whatever
    // KeyWord
    // possible content to be changed

    // file2.cs
    // ... whatever
    // possible content to be changed

    The goal is to apply the search replace only to the content in 'file1.cs While all this is easy to do in UltraEdit (which offers UNIX and Perl regex flavors in addition to its own syntax), I'd like to know if this is possible in VS. thanks, Bill

    «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

    Richard DeemingR 1 Reply Last reply
    0
    • B BillWoodruff

      I have been struggling with what Jeff Atwood calls VS's "bastardized regular expression syntax" [^]. Along the way, I found Peter Macej's very useful VS extension for multiple-line-search-replace [^]. It kind of warps my mind that VS, at this late date, doesn't have a built-in multi-line search and replace, and doesn't support a more "standard" RegEx syntax ! Using 'Find in Files' in VS it's easy to limit the search to specific types of files, like *.cs. Where I am stumped is how to select files that have specific text (keyword) content, and then apply S&R to only the matching files.

      // file1.cs
      // ... whatever
      // KeyWord
      // possible content to be changed

      // file2.cs
      // ... whatever
      // possible content to be changed

      The goal is to apply the search replace only to the content in 'file1.cs While all this is easy to do in UltraEdit (which offers UNIX and Perl regex flavors in addition to its own syntax), I'd like to know if this is possible in VS. thanks, Bill

      «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      BillWoodruff wrote:

      I have been struggling with what Jeff Atwood calls VS's "bastardized regular expression syntax"

      That only applies to VS2008 and earlier. Since VS2012, the search & replace uses standard .NET regular expressions[^].

      Using Regular Expressions in Visual Studio | Microsoft Docs[^]

      Visual Studio uses .NET Framework regular expressions to find and replace text. Before Visual Studio 2012, Visual Studio used custom regular expression syntax in the Find and Replace windows.

      BillWoodruff wrote:

      how to select files that have specific text (keyword) content, and then apply S&R to only the matching files.

      I don't think there's any way to do that in a single step, unless the content you're searching for is the content you want to replace. You might need to do a "find in files" to find and open the matching files, and then a S&R targeting the open files. Or were you wanting to limit the S&R based on the file names? I don't think they support regex; you'd need to use DOS-style wildcards instead.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      B 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        BillWoodruff wrote:

        I have been struggling with what Jeff Atwood calls VS's "bastardized regular expression syntax"

        That only applies to VS2008 and earlier. Since VS2012, the search & replace uses standard .NET regular expressions[^].

        Using Regular Expressions in Visual Studio | Microsoft Docs[^]

        Visual Studio uses .NET Framework regular expressions to find and replace text. Before Visual Studio 2012, Visual Studio used custom regular expression syntax in the Find and Replace windows.

        BillWoodruff wrote:

        how to select files that have specific text (keyword) content, and then apply S&R to only the matching files.

        I don't think there's any way to do that in a single step, unless the content you're searching for is the content you want to replace. You might need to do a "find in files" to find and open the matching files, and then a S&R targeting the open files. Or were you wanting to limit the S&R based on the file names? I don't think they support regex; you'd need to use DOS-style wildcards instead.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        B Offline
        B Offline
        BillWoodruff
        wrote on last edited by
        #3

        Thanks, Richard, My impression is that VS 2017 is using some form of JavaScript related regex syntax, which is still as non-standard as when Atwood made his comments in 2006. Of course, you can say it's .NET standard :) It took me two days to figure out one multi-line RegEx solution in VS. The MSDN docs were (as so often) useless. Thanks to Macej's extension ... which will generate a multi-line RegEx for you based on your selection in a code window ... this can be speeded up nicely. The tricky part of my goal here is applying the S&R recursively to folders/files that contain the "keyword" ... but, applying only to open files may have to suffice. It ought to be this simple: Solution.Files.Search(SearchOption.Recursive) .Where(file => file.Extension == ".cs") .Select(file => file.StartsWith(@"//Keyword\r\n")) .ReplaceInFiles(ReplaceOption.All)(@"stuff", @"newstuff); Yes, that is wishful thinking :) best, Bill

        «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

        M Z 2 Replies Last reply
        0
        • B BillWoodruff

          Thanks, Richard, My impression is that VS 2017 is using some form of JavaScript related regex syntax, which is still as non-standard as when Atwood made his comments in 2006. Of course, you can say it's .NET standard :) It took me two days to figure out one multi-line RegEx solution in VS. The MSDN docs were (as so often) useless. Thanks to Macej's extension ... which will generate a multi-line RegEx for you based on your selection in a code window ... this can be speeded up nicely. The tricky part of my goal here is applying the S&R recursively to folders/files that contain the "keyword" ... but, applying only to open files may have to suffice. It ought to be this simple: Solution.Files.Search(SearchOption.Recursive) .Where(file => file.Extension == ".cs") .Select(file => file.StartsWith(@"//Keyword\r\n")) .ReplaceInFiles(ReplaceOption.All)(@"stuff", @"newstuff); Yes, that is wishful thinking :) best, Bill

          «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

          M Offline
          M Offline
          Member 1263940
          wrote on last edited by
          #4

          BillWoodruff wrote:

          The tricky part of my goal here is applying the S&R recursively to folders/files that contain the "keyword" ... but, applying only to open files may have to suffice.

          In VS, you can select files with a name pattern. In Find/replace dialog, define it in Look at these file types. Moreover, you can select folders (and save this selection for later use) with [...] button next to Look in. And you can apply the search to open documents only. Select All Open Documents in Look in.

          B 1 Reply Last reply
          0
          • M Member 1263940

            BillWoodruff wrote:

            The tricky part of my goal here is applying the S&R recursively to folders/files that contain the "keyword" ... but, applying only to open files may have to suffice.

            In VS, you can select files with a name pattern. In Find/replace dialog, define it in Look at these file types. Moreover, you can select folders (and save this selection for later use) with [...] button next to Look in. And you can apply the search to open documents only. Select All Open Documents in Look in.

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Thanks, I am familiar with the uses of 'Find/Replace in Files you mention. But, what I am looking for is a way to apply a RegEx find/replace pattern recursively to files in nested folders with a keyword ... without having to open them all, or re-configure the current open code files in VS.

            «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

            1 Reply Last reply
            0
            • B BillWoodruff

              Thanks, Richard, My impression is that VS 2017 is using some form of JavaScript related regex syntax, which is still as non-standard as when Atwood made his comments in 2006. Of course, you can say it's .NET standard :) It took me two days to figure out one multi-line RegEx solution in VS. The MSDN docs were (as so often) useless. Thanks to Macej's extension ... which will generate a multi-line RegEx for you based on your selection in a code window ... this can be speeded up nicely. The tricky part of my goal here is applying the S&R recursively to folders/files that contain the "keyword" ... but, applying only to open files may have to suffice. It ought to be this simple: Solution.Files.Search(SearchOption.Recursive) .Where(file => file.Extension == ".cs") .Select(file => file.StartsWith(@"//Keyword\r\n")) .ReplaceInFiles(ReplaceOption.All)(@"stuff", @"newstuff); Yes, that is wishful thinking :) best, Bill

              «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

              Z Offline
              Z Offline
              ZurdoDev
              wrote on last edited by
              #6

              BillWoodruff wrote:

              Solution.Files.Search(SearchOption.Recursive) .Where(file => file.Extension == ".cs") .Select(file => file.StartsWith(@"//Keyword\r\n")) .ReplaceInFiles(ReplaceOption.All)(@"stuff", @"newstuff); Yes, that is wishful thinking :)

              Simple. Write your own VS Add-In. :-D

              There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

              B 1 Reply Last reply
              0
              • Z ZurdoDev

                BillWoodruff wrote:

                Solution.Files.Search(SearchOption.Recursive) .Where(file => file.Extension == ".cs") .Select(file => file.StartsWith(@"//Keyword\r\n")) .ReplaceInFiles(ReplaceOption.All)(@"stuff", @"newstuff); Yes, that is wishful thinking :)

                Simple. Write your own VS Add-In. :-D

                There are two kinds of people in the world: those who can extrapolate from incomplete data. There are only 10 types of people in the world, those who understand binary and those who don't.

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                Hi Ryan, I'm curious ... have you written any VS extensions yourself that operated on open project files ? A reasonable suggestion, but one I just don't have time to pursue. And, I can achieve what I want with UltraEdit. In my research on multi-line replacement, I came across a reference saying NotePad++ also has multi-line find.replace. thanks, Bill

                «Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.» Miss Piggy

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups