development

정규식을 어떻게 디버깅합니까?

big-blog 2020. 6. 14. 09:44
반응형

정규식을 어떻게 디버깅합니까? [닫은]


정규 표현식은 상당히 복잡해질 수 있습니다. 공백이 없으면 읽기가 어렵습니다. 디버거를 사용하여 정규 표현식을 밟을 수는 없습니다. 그렇다면 전문가들은 어떻게 복잡한 정규 표현식을 디버깅합니까?


RegexBuddy를 구매 하고 내장 된 디버그 기능을 사용 합니다 . 1 년에 두 번 이상 정규식으로 작업하면 시간을 절약 할 수 있습니다. RegexBuddy는 단순하고 복잡한 정규 표현식을 작성하고 다양한 언어로 코드를 생성 할 수 있도록 도와줍니다.

대체 텍스트

또한 개발자에 따르면이 도구는 WINE과 함께 사용할 때 Linux에서 거의 완벽하게 실행됩니다 .


Perl 5.10 사용시 use re 'debug';. (또는 debugcolor스택 오버플로에서 출력 형식을 올바르게 지정할 수 없습니다.)

$ perl -Mre = debug -e ' "foobar"= ~ / (.) \ 1 /'
REx "(.) \ 1"컴파일 중
최종 프로그램 :
   1 : OPEN1 (3)
   3 : REG_ANY (4)
   4 : CLOSE1 (6)
   6 : REF1 (8)
   8 : 끝 (0)
민렌 1
"foobar"와 REx "(.) \ 1"일치
   0 <> <foobar> | 1 : OPEN1 (3)
   0 <> <foobar> | 3 : REG_ANY (4)
   1 <f> <oobar> | 4 : CLOSE1 (6)
   1 <f> <oobar> | 6 : REF1 (8)
                                  실패한...
   1 <f> <oobar> | 1 : OPEN1 (3)
   1 <f> <oobar> | 3 : REG_ANY (4)
   2 <fo> <obar> | 4 : CLOSE1 (6)
   2 <fo> <obar> | 6 : REF1 (8)
   3 <foo> <bar> | 8 : END (0)
성공!
REx 해제 : "(.) \ 1"

또한, 당신은 할 수 그들을 더 읽을 수 있도록 정규 표현식에에 공백과 주석을 추가 할 수 있습니다. Perl에서는 /x수정자를 사용하여 수행됩니다 . 으로 pcre는이 PCRE_EXTENDED플래그.

"foobar" =~ /
    (.)  # any character, followed by a
    \1   # repeat of previously matched character
/x;

pcre *pat = pcre_compile("(.)  # any character, followed by a\n"
                         "\\1  # repeat of previously matched character\n",
                         PCRE_EXTENDED,
                         ...);
pcre_exec(pat, NULL, "foobar", ...);

잊어 버리지 않도록 다른 것을 추가하겠습니다 : debuggex

매우 시각적이기 때문에 좋습니다. Debuggex 정규식 헬퍼 사진


정규식에 갇 히면 일반적으로 https://regexr.com/

문제가있는 곳을 빠르게 테스트하기에 적합합니다.


나는 Kodos -The Python Regular Expression Debugger를 사용합니다.

Kodos is a Python GUI utility for creating, testing and debugging regular expressions for the Python programming language. Kodos should aid any developer to efficiently and effortlessly develop regular expressions in Python. Since Python's implementation of regular expressions is based on the PCRE standard, Kodos should benefit developers in other programming languages that also adhere to the PCRE standard (Perl, PHP, etc...).

(...)

대체 텍스트

Runs on Linux, Unix, Windows, Mac.


I think they don't. If your regexp is too complicated, and problematic to the point you need a debugger, you should create a specific parser, or use another method. It will be much more readable and maintainable.


There is an excellent free tool, the Regex Coach. The latest version is only available for Windows; its author Dr. Edmund Weitz stopped maintaining the Linux version because too few people downloaded it, but there is an older version for Linux on the download page.


I've just seen a presentation of Regexp::Debugger by its creator: Damian Conway. Very impressive stuff: run inplace or using a command line tool (rxrx), interactively or on a "logged" execution file (stored in JSON), step forward and backward at any point, stop on breakpoints or events, colored output (user configurable), heat maps on regexp and string for optimization, etc...

Available on CPAN for free: http://search.cpan.org/~dconway/Regexp-Debugger/lib/Regexp/Debugger.pm


I use this online tool to debug my regex:

http://www.regextester.com/

But yeah, it can't beat RegexBuddy.


I debug my regexes with my own eyes. That's why I use /x modifier, write comments for them and split them in parts. Read Jeffrey Friedl's Mastering Regular Expressions to learn how to develop fast and readable regular expressions. Various regex debugging tools just provoke voodoo programming.


As for me I usually use pcretest utility which can dump the byte code of any regex, and usually it is much more easier to read (for me at least). Example:

PCRE version 8.30-PT1 2012-01-01

  re> /ab|c[de]/iB
------------------------------------------------------------------
  0   7 Bra
  3  /i ab
  7  38 Alt
 10  /i c
 12     [DEde]
 45  45 Ket
 48     End
------------------------------------------------------------------

I use:

http://regexlib.com/RETester.aspx

You can also try Regex Hero (uses Silverlight):

http://regexhero.net/tester/


If I'm feeling stuck, I like to go backward and generate the regex directly from a sample text using txt2re (although I usually end up tweaking the resulting regex by hand).


If you're a Mac user, I just came across this one:

http://atastypixel.com/blog/reginald-regex-explorer/

It's free, and simple to use, and it's been a great help for me to get to grips with RegExs in general.


Have a look at the (non-free) tools on regular-expressions.info. RegexBuddy in particular. Here is Jeff Atwood's post on the subject.


Writing reg exes using a notation like PCREs is like writing assembler: it's fine if you can just see the corresponding finite state automata in your head, but it can get difficult to maintain very quickly.

The reasons for not using a debugger are much the same as for not using a debugger with a programming language: you can fix local mistakes, but they won't help you solve the design problems that led you to make the local mistakes in the first place.

The more reflective way is to use data representations to generate regexps in your programming language, and have appropriate abstractions to build them. Olin Shiver's introduction to his scheme regexp notation gives an excellent overview of the issues faced in designing these data representations.


I often use pcretest - hardly a "debugger" but it works over a text-only SSH connection and parses exactly the regex dialect I need: my (C++) code links to libpcre, so there's no difficulty with subtle differences in what's magic and what isn't, etc.

In general I agree with the guy above to whom needing a regex debugger is a code smell. For me the hardest about using regexes is usually not the regex itself, but the multiple layers of quoting needed to make them work.


I often use Ruby based regexp tester Rubular

and also in Emacs use M-x re-builder

Firefox also has a useful extension


I use the Rx Toolkit included with ActiveState Komodo.


You could try this one http://www.pagecolumn.com/tool/regtest.htm


나를 위해, 정규식을 눈여겨 본 후에 (나는 상당히 유창하고 거의 항상 / x 또는 동등한 것을 사용하기 때문에) 약간의 퇴화 일치 (예 : 과도하게 역행하는 것)가 있는지 확실하지 않으면 테스트 대신 디버그 할 수 있습니다 예를 들어 연산자의 욕심을 수정하여 이러한 문제를 해결할 수 있는지 확인하십시오.

이를 위해 위에서 언급 한 방법 중 하나를 사용합니다 : pcretest, RegexBuddy (현재 직장에서 라이센스가 부여 된 경우) 또는 유사하며 때로는 C # regexes에서 작업하는 경우 Linqpad에서 시간을 정합니다.

(펄 트릭은 저에게 새로운 트릭이므로 내 정규식 툴킷에도 추가 할 것입니다.)

참고 URL : https://stackoverflow.com/questions/2348694/how-do-you-debug-a-regex

반응형