Why does the order of alternatives matter in regex?

Athari

Code

using System;
using System.Text.RegularExpressions;

namespace RegexNoMatch {
    class Program {
        static void Main () {
            string input = "a foobar& b";
            string regex1 = "(foobar|foo)&?";
            string regex2 = "(foo|foobar)&?";
            string replace = "$1";
            Console.WriteLine(Regex.Replace(input, regex1, replace));
            Console.WriteLine(Regex.Replace(input, regex2, replace));
            Console.ReadKey();
        }
    }
}

Expected output

a foobar b
a foobar b

Actual output

a foobar b
a foobar& b

Question

Why does replacing not work when the order of "foo" and "foobar" in regex pattern is changed? How to fix this?

p.s.w.g

The regular expression engine tries to match the alternatives in the order in which they are specified. So when the pattern is (foo|foobar)&? it matches foo immediately and continues trying to find matches. The next bit of the input string is bar& b which cannot be matched.

In other words, because foo is part of foobar, there is no way (foo|foobar) will ever match foobar, since it will always match foo first.

때때로 이것은 실제로 매우 유용한 트릭이 될 수 있습니다. 패턴은 (o|a|(\w))캡처 할 수 \wa또는 o다른 :

Regex.Replace("a foobar& b", "(o|a|(\\w))", "$2") // fbr& b

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does the order of applying advice matter?

분류에서Dev

why does the order of variable declaring matter?

분류에서Dev

Why does the malloc order matter when a struct is sufficiently sized?

분류에서Dev

Why does Order matter in Kwarg parameters in MagicMock asserts?

분류에서Dev

Does the order of var in the javascript context matter?

분류에서Dev

Does the order of entries in the routing table matter?

분류에서Dev

Why does hyper threaded or Multi-threaded CPU matter?

분류에서Dev

Alternatives to java.util.regex

분류에서Dev

Why does this SQL order null values last?

분류에서Dev

Why does my C program print out the same output no matter what I put for the input?

분류에서Dev

is there a convention for localhost ports ... does it matter?

분류에서Dev

Why MySQL does not return rows in same order as appeared in IN clause?

분류에서Dev

Hibernation does not work, but what are my alternatives?

분류에서Dev

Why both of if statements are executed no matter what the input is?

분류에서Dev

Does it matter that my deb version is not Debian native?

분류에서Dev

Why does my regex has a strange behavior? Is this a bug?

분류에서Dev

Why does sub replace only one character with a regex?

분류에서Dev

Initialization of a wchar_t array with wmemset. Does encoding matter?

분류에서Dev

Does the ST4000NM0033 really use 512 byte sectors, and does it matter?

분류에서Dev

how does ORDER BY take precedence?

분류에서Dev

Does it matter if i started training of my keras model with a big value of the loss function

분류에서Dev

Regex in powershell does not work as expected

분류에서Dev

Alternatives to the HitTest()

분류에서Dev

Alternatives to wget

분류에서Dev

Alternatives to Cheese

분류에서Dev

Why does this program loop?

분류에서Dev

Why and how does this work?

분류에서Dev

Why does '/' have an '..' entry?

분류에서Dev

Why does facebook not like this

Related 관련 기사

  1. 1

    Why does the order of applying advice matter?

  2. 2

    why does the order of variable declaring matter?

  3. 3

    Why does the malloc order matter when a struct is sufficiently sized?

  4. 4

    Why does Order matter in Kwarg parameters in MagicMock asserts?

  5. 5

    Does the order of var in the javascript context matter?

  6. 6

    Does the order of entries in the routing table matter?

  7. 7

    Why does hyper threaded or Multi-threaded CPU matter?

  8. 8

    Alternatives to java.util.regex

  9. 9

    Why does this SQL order null values last?

  10. 10

    Why does my C program print out the same output no matter what I put for the input?

  11. 11

    is there a convention for localhost ports ... does it matter?

  12. 12

    Why MySQL does not return rows in same order as appeared in IN clause?

  13. 13

    Hibernation does not work, but what are my alternatives?

  14. 14

    Why both of if statements are executed no matter what the input is?

  15. 15

    Does it matter that my deb version is not Debian native?

  16. 16

    Why does my regex has a strange behavior? Is this a bug?

  17. 17

    Why does sub replace only one character with a regex?

  18. 18

    Initialization of a wchar_t array with wmemset. Does encoding matter?

  19. 19

    Does the ST4000NM0033 really use 512 byte sectors, and does it matter?

  20. 20

    how does ORDER BY take precedence?

  21. 21

    Does it matter if i started training of my keras model with a big value of the loss function

  22. 22

    Regex in powershell does not work as expected

  23. 23

    Alternatives to the HitTest()

  24. 24

    Alternatives to wget

  25. 25

    Alternatives to Cheese

  26. 26

    Why does this program loop?

  27. 27

    Why and how does this work?

  28. 28

    Why does '/' have an '..' entry?

  29. 29

    Why does facebook not like this

뜨겁다태그

보관