0

Is there a way to find and replace all occurrences of the following in all default.aspx.vb pages in our web site:

Private Function fExampleA() As Boolean
‘Existing code here
‘Existing code here
‘Existing code here etc
End Function

With:

Private Function fExampleA() As Boolean
‘New code here
‘New code here
‘New code here etc
End Function

UPDATE

Yes, the new function is envisaged to be:

Private Function fExampleA() As Boolean
 
 Dim c as new cGeneral
 With c
 .fCommonExampleA()
 End With
 
End Function
asked Jan 23, 2025 at 19:54
7
  • 1
    Well, this begs the question why there is not one global function that all the pages can use? Why copy code over and over? Anyway, I would do a shift-ctrl-f (that's a global find). Type in "Function FExample", and now you have a "list" of all locations of that function, and thus can work your way though that list - and use a "paste" into that routine. But really, why not have ONE function that all pages call? Commented Jan 23, 2025 at 20:17
  • @AlbertD.Kallal Yes, a refactoring to a new call to a common function as you mentioned. We’ve got hundreds of occurrences of the function so I was hoping for an automated way to replace them all. Commented Jan 23, 2025 at 20:26
  • Well, the global shift-ctrl-f to find and search the whole project probably is a good start. I suppose you could consider a global find/replace, but that would assume an exact match. There can't be that many instances here - and the shift-ctrl-f to global find will produce a VERY nice "hit list" of results, and that list can be used to work your way though each instance...... Commented Jan 23, 2025 at 20:31
  • @AlbertD.Kallal Perhaps a global file find and replace using regex could replace the content of all text starting with "Private Function fExampleA() As Boolean" and ending with "End Function" with the new function code. Commented Jan 23, 2025 at 20:37
  • 1
    Hum, well, you could try pasting in the whole routine match - see if it works. I just think doing one by one would not take all that long "once" you have the search hit list. So, often people are not aware of the shift-ctrl-f for the global find (and/or replace). I think once you done the global find, then working though the hit-list is a better approach then actually using find + replace. Commented Jan 23, 2025 at 20:52

1 Answer 1

1

Find: Private Function (f\w+)\(\) As Boolean(.*\n)*?\s*End Function

Replace: Private Function 1ドル() As Boolean \n\t Dim c as new cGeneral \n\t With c \n \t .1ドル()\n\t End With \n End Function

This regular expression can achieve the desired result, but it assumes that the code format is consistent with the example you provided. If it's not, you will need to make adjustments, such as finding common points in the function name section. The (.*\n)*?\s*End Function part is used for multi-line searches and doesn't require modification.

During replacement, implicit grouping in the regular expression is used, and 1ドル is used to call group 1, which is the matched function name.

answered Jan 24, 2025 at 5:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.