In Regex, what is the difference between (?!^)\G and \G(?!^)?

why

Given these two Regex:

(?!^)\G

and

\G(?!^)

What's the difference between negative lookahead after and before \G anchor?

Timwi

Logically, they come out to be the same thing. They are functionally equivalent. The (?!^) and the \G check two different conditions at the same location in the string, so it makes no logical difference what order the conditions are checked in.

The conditions are:

  • (?!^) = “we are not at the beginning of the input string”
  • \G = “we are in the location where the previous match ended”

However, in terms of performance, I suspect (though I haven’t tested) that the latter is faster. I would expect the regular expression engine to have an optimisation so that a regex that starts with \G would only be executed from the previous match’s end onwards, while the other one would go through the whole string “looking for” the location of the previous match.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is an actual difference between redux and a state machine (e.g. xstate)?

From Java

What is the difference between g++ and gcc?

From Java

What is the difference between gcc/g++ and cc1/cc1plus?

From Dev

What is the difference between :g and :%s commands in vim

From Dev

In Regex, what is the difference between (?!^)\G and \G(?!^)?

From Dev

What is the difference between clang (and LLVM) and gcc / g++?

From Dev

In guava, what is the difference (if any) between Lists.newArrayList() and e.g. Ints.asList() for primitive types

From Dev

Is /a/g in {}/a/g a regex or a division?

From Dev

What is the difference between G1GC options -XX:ParallelGCThreads vs -XX:ConcGCThreads

From Dev

What's the difference between $/ and $¢ in regex?

From Dev

Difference between adduser and usermod -G -a

From Dev

What's the difference between regex [-+]? and (-|+)?

From Dev

What is the difference between 2.4G and 5G wireless?

From Dev

What is the difference between clang (and LLVM) and gcc / g++?

From Dev

Is /a/g in {}/a/g a regex or a division?

From Dev

What is the Difference between Regex `[]*` and `[]+`?

From Dev

Can someone advise on DL380 G5 RAM, what is the difference between these DIMMs?

From Dev

What's the difference between using shell command (e.g. foo) directly and using $(foo)?

From Dev

What is the difference between -g and -G options in useradd

From Dev

What is the difference between (.*?) and (.*)? in regex?

From Dev

What is the difference between "shutdown /r" and "shutdown /g"?

From Dev

Difference between set -g and set -x

From Dev

What is the difference between calling "id -G" and "id -G -r"?

From Dev

perl-rename - What is the difference between /g' and /'?

From Dev

difference between 'usermod -aG' and 'usermod -G' options

From Dev

What’s the difference between 'gcc -g' and 'gcc -g3'?

From Dev

g++ Difference Between Bash For Windows and Ubuntu

From Dev

XML - What is the difference in use and function between e.g. <person name="JohnDoe"></person> and <person>JohnDoe</person>?

From Dev

What's the difference between functions starting with G_ and GTK_?

Related Related

  1. 1

    What is an actual difference between redux and a state machine (e.g. xstate)?

  2. 2

    What is the difference between g++ and gcc?

  3. 3

    What is the difference between gcc/g++ and cc1/cc1plus?

  4. 4

    What is the difference between :g and :%s commands in vim

  5. 5

    In Regex, what is the difference between (?!^)\G and \G(?!^)?

  6. 6

    What is the difference between clang (and LLVM) and gcc / g++?

  7. 7

    In guava, what is the difference (if any) between Lists.newArrayList() and e.g. Ints.asList() for primitive types

  8. 8

    Is /a/g in {}/a/g a regex or a division?

  9. 9

    What is the difference between G1GC options -XX:ParallelGCThreads vs -XX:ConcGCThreads

  10. 10

    What's the difference between $/ and $¢ in regex?

  11. 11

    Difference between adduser and usermod -G -a

  12. 12

    What's the difference between regex [-+]? and (-|+)?

  13. 13

    What is the difference between 2.4G and 5G wireless?

  14. 14

    What is the difference between clang (and LLVM) and gcc / g++?

  15. 15

    Is /a/g in {}/a/g a regex or a division?

  16. 16

    What is the Difference between Regex `[]*` and `[]+`?

  17. 17

    Can someone advise on DL380 G5 RAM, what is the difference between these DIMMs?

  18. 18

    What's the difference between using shell command (e.g. foo) directly and using $(foo)?

  19. 19

    What is the difference between -g and -G options in useradd

  20. 20

    What is the difference between (.*?) and (.*)? in regex?

  21. 21

    What is the difference between "shutdown /r" and "shutdown /g"?

  22. 22

    Difference between set -g and set -x

  23. 23

    What is the difference between calling "id -G" and "id -G -r"?

  24. 24

    perl-rename - What is the difference between /g' and /'?

  25. 25

    difference between 'usermod -aG' and 'usermod -G' options

  26. 26

    What’s the difference between 'gcc -g' and 'gcc -g3'?

  27. 27

    g++ Difference Between Bash For Windows and Ubuntu

  28. 28

    XML - What is the difference in use and function between e.g. <person name="JohnDoe"></person> and <person>JohnDoe</person>?

  29. 29

    What's the difference between functions starting with G_ and GTK_?

HotTag

Archive