解析文本文件的行,其中值由不同数量的空白字符分隔 [英] Parse lines of a text file where values are separated by a varying number of whitespace characters

查看:39
本文介绍了解析文本文件的行,其中值由不同数量的空白字符分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在不同的数组中获取公司名称及其股票代码.这是我存储在 txt 文件中的数据:

I need to get the company name and its ticker symbol in different arrays. Here is my data which is stored in a txt file:

3M Company      MMM
99 Cents Only Stores    NDN
AO Smith Corporation    AOS
Aaron's, Inc.   AAN

等等

我将如何使用正则表达式或其他一些技术来做到这一点?

How would I do this using regex or some other techniques?

推荐答案

遍历每一行,用正则表达式收集数据:

Iterate over each line, and collect the data with a regular expression:

^(.+?)\s+([A-Z]+)$

反向引用 $1 将包含公司名称,$2 将包含股票代码.

The backreference $1 will contain the company name, $2 will contain the ticker symbol.

您还可以使用两个或三个空格分隔符将字符串分成两部分,然后修剪生成的两个字符串.这仅在您确定公司名称和股票代码始终由足够多的空格分隔并且公司名称本身不包含那么多空格时才有效.

You can also split the string in two with a two or three-space delimiter and trim the resulting two strings. This only works if you are sure the company name and ticker symbol are always separated by enough spaces, and the company name itself doesn't contain that amount of spaces.

这篇关于解析文本文件的行,其中值由不同数量的空白字符分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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