帮助正则表达式拆分 [英] Help with regular expression split

查看:80
本文介绍了帮助正则表达式拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望通过插入换行符来格式化一长行文本,打印输出包含最多80个字符的小行。但我不允许在序列中拆分<(>"。这里,我必须先拆分,尽可能长一段。

长行不包含换行符本身。


我用不同的正则表达式尝试了很长时间,但是我无法正确使用它。现在我想出了一个解决方案,但我不知道是不是总是有效还是有更简单的解决方案?


为简化任务,我选择x作为换行符,最大行长= 4个字符。 />
它似乎在这里工作:

I want to format a long line of text by inserting newline-characters in a way that the printout contains small rows with maximum 80 chars. But I am not allowed to split inside the sequence "<(>". Here, I must split before, making the line as long a possible.
The long line does not contain a newline character itself.

I tried long time with different regular expressions, but I could not get it right. Now I came up with a solution, but I don''t know if it will always work or if there is a much simpler solution?

To simplify the task, I choose "x" as the newline character and the maximum line length=4 characters.
It seems to work here:

展开 | 选择 | Wrap | <温泉n class =codeLinkonclick =LineNumbers(this);>行号

推荐答案

正则表达式让我的烟草变得湿润,让我的啤酒变成啤酒酸。可以用简单的英文描述你想要分割字符串的字符吗?


亲切的问候,


Jos
Regular expressions make my tobacco go moist and make my beer sour. Can
you describe in plain English on what character(s) you want to split your String?

kind regards,

Jos


我想分割任何字符,以便该行为4个字符(最后一行除外):

" 1234567890" - > 1234,5678,90。


有一个例外:字符串<(>"不应拆分,因为它们是ITF文本中的控制字符,如HTML标签。在这种情况下,我必须先拆分,使行少于4个字符:

" 12<(> 67890" - >12,<(> 6"," 7890"

错误 - >"" 12<(",">> 678",""在这里,控制序列被分成<(&和>"。

不正确 - >" 1"""""" (>"," 6789"," 0" //这里第一部分太小,只有1个字符。它应该是贪婪的,也是第二个字符。


另一个例子:

" 123<(> 7890" - >" 123",""(> 7"," 890"

不正确 - >" 123<","(> 78"," 90" //她的e,控制序列分为<和<。和(>"。


控制序列必须准确。如果序列不完整,则允许拆分。在这里,您可以拆分任何字符:

"<(< ..<< ..<)..<("
I want to split at any character so that the line is 4 characters (except the last line):
"1234567890" --> "1234", "5678", "90".

There is one exception: The string "<(>" should no be split, because they are control-characters in ITF-text, like HTML tags. In this case, I must split before, making the line less than 4 characters.:
"12<(>67890" --> "12", "<(>6", "7890"
incorrect --> "12<(", ">678", "90" // here, the control sequence is split in "<(" and ">".
incorrect --> "1", "2<(>", "6789", "0" // here the first part is too small, only 1 character. It should be greedy and also take the second character.

Another example:
"123<(>7890" --> "123", "<(>7", "890"
incorrect --> "123<", "(>78", "90" // here, the control sequence is split in "<" and "(>".

The control sequence must be exact. If the sequence is incomplete, a split is allowed. Here, you can split at any character:
"<(<..<< .. <) ..<("


说到文本背景,定期表达式被严重高估了。

人们期待他们的魔力。你不能简单地拆分<(>"并处理

结果数组有点问题吗?


亲切的问候,


Jos
When it comes to textual context, regular expressions are heavily overrated and
people expect magic from them. Can''t you simply split on "<(>" and process the
resulting array of Strings a bit?

kind regards,

Jos


这篇关于帮助正则表达式拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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