0

I'm trying to delete the virtual keyword in the derived method which is an override. Should I use virtual, override, or both keywords?

now I have the several scenario:

virtual void test1() const override;

virtual int test2() override { xxx }

virtual void test3(int p1,
                    int p2,
                    int p3) const override;
virtual void test4(int p1,
                    int p2,
                    int p3,
                    int p4) const override;

virtual void testn(int p1,
                    int p2,
                    int p3,
                    int p4,
                    ......,
                    int pn) const override;

--> I need to delete the virtual keyword for the whole project code(hundreds of files)

void test1() const override;

int test2() override { xxx }

void test3(int p1,
                    int p2,
                    int p3) const override;
void test4(int p1,
                    int p2,
                    int p3,
                    int p4) const override;

void testn(int p1,
                    int p2,
                    int p3,
                    int p4,
                    ......,
                    int pn) const override;

Because there are lots of places that need to modify, I plan to use RegExp to do it. I try the following RegExp:

\bvirtual\b.*\s.*\s.*\s.*\s.*\s.*\s.*\boverride\b

but it seems \s.* need to know the number, is there anyone familiar with RegExp who can help me how to write the expression? and also how to replace and delete the virtual keyword by RegExp? [![enter image description here][1]][1]

irwin
  • 23
  • 5
  • Honestly, if it were me I'd solve the easy ones with regex, and then assess how many of the other cases there actually are. In my experience, walking through find-all results with a reasonable search that's likely to find lines which _might_ need fixing and then manually fixing them is just a bit of once-off grunt work that doesn't take too long. And yeah, I've done that kind of thing on hundreds of files before. A coffee, some good music, and an hour or two and never have to worry about it again. – paddy Mar 30 '22 at 03:31
  • thx, paddy. it's a good idea. but how to replace the virtual to null by the regexp? do you know that? – irwin Mar 30 '22 at 03:36
  • Yeah, you put all the stuff you care about in a capture group and then use the capture group only as the replacement string. – paddy Mar 30 '22 at 03:38
  • As an aside, are you sure your expression is supposed to be matching only anything that contains 6 or more whitespace characters between the `virtual` and the `override` keyword? It seems a bit arbitrary to me.... Because what about `virtual foo() override` -- that only contains 2 whitespace characters and will not be matched. – paddy Mar 30 '22 at 03:42
  • right now I only want to replace the first scenario: void test1() const override; this scenario is 80% of the whole project. actually, \s used here I meant to match the \n, but it did not work as I thought. – irwin Mar 30 '22 at 05:39
  • could you tell me how to replace: \bvirtual\b.*\boverride\b the virtual to nothing? – irwin Mar 30 '22 at 05:42
  • A simple search would be `\bvirtual\b(.*\boverride\b)` and the replacement would probably be either `$1` or `\1` to expand the first matching group. The syntax usually depends on whatever software you're using. Read its documentation for regex replacement. – paddy Mar 30 '22 at 05:46
  • thx, paddy, I got it from a youtube : https://www.youtube.com/watch?v=qYuei-jBDZQ search pattern: (\bvirtual\b)(.*)(\boverride\b) replace pattern: $2$3 – irwin Mar 30 '22 at 06:16
  • Overkill, but okay. – paddy Mar 30 '22 at 06:18
  • Pretty sure clang-format or other existing tool might remove or detect redundant `virtual`. – Jarod42 Mar 30 '22 at 10:16
  • regex might be useful, but it is not reliable as parsing C++ is complicated. – Jarod42 Mar 30 '22 at 10:17
  • This sounds like an utter waste of time. – Pete Becker Mar 30 '22 at 13:54

1 Answers1

0

Consider

virtual void test1() const override;

virtual int test2() override { xxx } //an example of commentary

virtual void test3(int p1,
                    int p2,
                    int p3) const override;
virtual void test4(int p1,
                    int p2,
                    int p3,
                    int p4) const override;

virtual void testn(int p1,
                    int p2,
                    int p3,
                    int p4,
                    ......,
                    int pn) const override;

and regex

\bvirtual\b\s*(.+?)\s*\boverride\s*(.+?)

the regex flags are /gms.

The result is

void test1() const;

int test2(){ xxx } //an example of commentary

void test3(int p1,
                    int p2,
                    int p3) const;
void test4(int p1,
                    int p2,
                    int p3,
                    int p4) const;

void testn(int p1,
                    int p2,
                    int p3,
                    int p4,
                    ......,
                    int pn) const;

Please, use this regex 101 link for further details and explanation, if needed.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Suthiro
  • 1,210
  • 1
  • 7
  • 18
  • Notice that you might remove wrongly some `virtual` [Demo](https://regex101.com/r/mpoVY7/1). – Jarod42 Mar 30 '22 at 10:14
  • thx, Demo. I need to take time to consume your expression. I'm newer to regex. but for the pure virtual, it seems to delete the virtual too. in this scenario: virtual void test() = 0; should keep the virtual keyword. – irwin Mar 31 '22 at 06:35
  • yesterday I try to several times to delete the virtual already, because the project manager push me.. search: (\bvirtual \b)(.*)(\boverride\b) , replace: $2$3 will delete 80% of the virtual. and search: (\bvirtual \b)(.*)(\n.*)(\boverride\b) repace: $2$3$4 will solve second scenario. and so on. my way is not totally correct, I mannuly remove some not match results. – irwin Mar 31 '22 at 06:40