Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:37:48+00:00 2026-06-17T13:37:48+00:00

I am trying to replace public methods to protected methods for methods that have

  • 0

I am trying to replace public methods to protected methods for methods that have a comment.

This because I am using phpunit to test some of those methods, but they really don’t need to be public, so I’d like to switch them on the production server and switch back when testing.

Here is the method declaration:

public function extractFile($fileName){ //TODO: change to protected

This is the regexp:

(?<ws>^\s+)(?<pb>public)(?<fn>[^/\n]+)(?<cm>//TODO: change to protected)

If I replace it with:

\1protected\3\//TODO: change back to public for testing

It seems to be working, but what I cannot get to work is naming the replace with. I have to use \1 to get the first group. Why name the groups if you can’t access them in the replacing texts? I tried things like <ws>, $ws, $ws, but that doesn’t work.

What is the replacing text if I want to replace \1 with the <ws> named group?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:37:49+00:00Added an answer on June 17, 2026 at 1:37 pm

    The ?<ws> named group syntax is the same as that used by .NET/Perl. For those regex engines the replacement string reference for the named group is ${ws}. This means your replacement string would be:

    ${ws}protected${fn}\//TODO: change back to public for testing

    The \k<ws> reference mentioned by m.buettner is only used for backreferences in the actual regex.

    Extra Information:

    It seems like Geany also allows use of Python style named groups:

    • ?P<ws> is the capturing syntax
    • \g<ws> is the replacement string syntax
    • (?P=ws) is the regex backreference syntax

    EDIT:

    It looks my hope for a solution didn’t pan out. From the manual,

    A subpattern can be named in one of three ways: (?…) or (?’name’…) as in Perl, or (?P…) as in Python. References to capturing parentheses from other parts of the pattern, such as backreferences, recursion, and conditions, can be made by name as well as by number.

    And further down:

    Back references to named subpatterns use the Perl syntax \k or \k’name’ or the Python syntax (?P=name).

    and

    A subpattern that is referenced by name may appear in the pattern before or after the reference.

    So, my inference of the syntax for using named groups was correct. Unfortunately, they can only be used in the matching pattern. That answers your question "Why name groups…?".

    How stupid is this? If you go to all the trouble to implement named groups and their usage in the matching pattern, why not also implement usage in the replacement string?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace a value with a passed value using javascript or
I have been trying to replace a word in a text file with a
I have developed a blog application of sorts that I am trying to allow
I'm trying to write some basic example from springsource.org and also to replace SimpleFormController
I'm trying to mock a singleton using this method described by the author of
I'm trying to make an AJAX call to a model that I have in
I'm trying to write some code to help unit test WCF services. These services
I have this scenario in which memory conservation is paramount. I am trying to
Here's a controller I'm trying to replace: @RequestMapping(/user) public @ResponseBody UserRsp callUserService(Principal principal) {
I am trying to replace a nasty LINQ 2 SQL hit with some dapper

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.