按固定宽度将字符串拆分为子字符串 [英] Split a string into sub strings by fixed width
问题描述
我有看起来像这样的数据...
I've got data which looks like this...
1 TESTAAA SERNUM A DESCRIPTION
2 TESTBBB ANOTHR ANOTHER DESCRIPTION
3 TESTXXX BLAHBL
我的问题是,将数据拆分成较小的子字符串的最有效方法是什么,因为将有数百行.另外,某些行将缺少最后一列.我尝试做正则表达式,但是使用宽度模板却失败了.上面的数据应细分为这些字段(下面列出的每列的长度)
My question is, what is the most efficient way to split this data into it's smaller substrings, as there will be hundreds of lines. Also, some of the lines will be missing the last column. I tried to do regex but wasn't successful with the pattern I used for widths. The data above should break down into these fields (length of each column listed below)
{id} {firsttext} {serialhere} {description}
4 22 6 30+
任何人都可以伸出援手或提出一个好的正则表达式匹配模式来提取信息吗?
Can anyone lend a hand or suggest a good regex matching pattern to extract the information?
谢谢,西蒙
推荐答案
尝试以下正则表达式:
(.{4})(.{22})(.{6})(.+)?
如果值始终为非空并用空格分隔(也就是说,它们不会相互碰到),则尝试使用更简单的方法
If the values are always nonempty and separated with whitespace (that is, they don't run into each other), then try something simpler like
line.Split(" ")
这篇关于按固定宽度将字符串拆分为子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!