REGEX-每隔4个字符插入一个空格,并每隔40个字符插入一个换行符 [英] REGEX - Insert space after every 4 characters, and a line break after every 40 characters

查看:116
本文介绍了REGEX-每隔4个字符插入一个空格,并每隔40个字符插入一个换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的字符串(22000多个字符),编码后的文本.该代码由数字[0-9]和小写字母[a-z]组成.我需要一个正则表达式在每4个字符后插入一个空格,并在每40个字符后插入一个换行符[\ n].有什么想法吗?

I have a huge string (22000+ characters) of encoded text. The code is consisted of digits [0-9] and lower case letters [a-z]. I need a regular expression to insert a space after every 4 characters, and one to insert a line break [\n] after every fourty characters. Any ideas?

推荐答案

好吧,正则表达式本身不会插入空格,因此我假设您使用的任何语言都有基于该插入的命令找到一个正则表达式.

Well, a regexp in itself doesn't insert a space, so I'll assume you have some command in whatever language you're using that inserts based on finding a regexp.

因此,找到4个字符并找到40个字符:在一般的正则表达式中这不是很漂亮(除非您的特定实现具有表示数字的好方法).要查找4个字符,请使用

So, finding 4 characters and finding 40 characters: that's not pretty in general regular expressions (unless your particular implementation has nice ways to express numbers). For finding 4 characters, use

....

因为典型的正则表达式查找器使用最大的munch,然后从一个正则表达式的末尾开始向前搜索并再次最大程度地进行munch,因此会将您的字符串分成4个字符.丑陋的部分是在标准正则表达式中,您必须使用

Because typical regexp finders use maximal munch, then from the end of one regexp, search forward and maximally munch again, that'll chunk your string into 4 character pieces. The ugly part is that in standard regular expressions, you'll have to use

........................................

查找40个字符的块,尽管我会注意到,如果您先运行4个字符,则必须运行

to find chuncks of 40 characters, although I'll note that if you run your 4 character one first, you'll have to run

..................................................

.... .... .... .... .... .... .... .... .... .... 

考虑您已经放入的空间.

to account for the spaces you've already put in.

句点可以找到任何字符,但是鉴于您只使用[0-9 | az],如果您需要确保没有其他内容,可以使用该正则表达式代替每个句点,我只是避免甚至更大.

The period finds any characters, but given that you're only using [0-9|a-z], you could use that regexp in place of each period if you need to ensure nothing else slipped in, I was just avoiding making it even more gross.

您可能会注意到,regexp有一些限制.看看 Chomsky层次结构,以真正了解其理论局限性.

As you may be noting, regexp have some limitations. Take a look at the Chomsky hierarchy to really get into their theoretical limitations.

这篇关于REGEX-每隔4个字符插入一个空格,并每隔40个字符插入一个换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆