开发者

getting started with lex

开发者 https://www.devze.com 2022-12-30 16:53 出处:网络
I need to format some hexdump like this: 00010: 02 03 04 05 00020:开发者_如何学编程 02 03 04 08

I need to format some hexdump like this:

00010: 02 03 04 05
00020:开发者_如何学编程 02 03 04 08
00030: 02 03 04 08

00010: 02 03 04 05
00020: 02 03 04 05
02 03 04 05
02 03 04 08
‍

to

02 03 04 05
02 03 04 08
02 03 04
02 03 04 05
02 03 04 05
02 03 04 05
02 03 04
  • remove the address fields, if present

  • remove any 08 at the end of a paragraph (followed by an empty line)

  • remove any empty lines

How can this be done using lex? thanks!


It cannot be done directly using lex. Lex is a tokenizer, not a parser.

In all honesty, it can be done using a regular expression and doesn't need the complexity of a scanner generator + parser generator.


If you slurp in the whole file as one string, I think these regular expressions will do what you want (written for Perl, but not tested):

s/^\d{4}: //mg
s/ 08\n\s*\n/\n/g
s/^\s*$//mg
0

精彩评论

暂无评论...
验证码 换一张
取 消