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
-
1Well, 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?Albert D. Kallal– Albert D. Kallal2025年01月23日 20:17:32 +00:00Commented 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.user1946932– user19469322025年01月23日 20:26:04 +00:00Commented 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......Albert D. Kallal– Albert D. Kallal2025年01月23日 20:31:57 +00:00Commented 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.user1946932– user19469322025年01月23日 20:37:29 +00:00Commented Jan 23, 2025 at 20:37
-
1Hum, 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.Albert D. Kallal– Albert D. Kallal2025年01月23日 20:52:38 +00:00Commented Jan 23, 2025 at 20:52
1 Answer 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.
Comments
Explore related questions
See similar questions with these tags.