Perl:删除特定点的字符。
我尝试搜索已经提出的问题,但似乎找不到任何东西。我确信它做起来非常简单,但我对 Perl 完全陌生。
我想要做的是删除字符串中的字符直到某个点。例如,我有:
Parameter1 : 0xFFFF
我想做的是删除“Parameter1:”并只留下“0xFFFF”。如果有人可以提供帮助并给出所使用的运算符的简单解释,那就太好了。
I've tried searching through questions already asked, but can't seem to find anything. I'm sure its incredibly simple to do, but I am completely new to Perl.
What I am trying to do is remove characters in an string up to a certain point. For example, I have:
Parameter1 : 0xFFFF
and what I would like to do is remove the "Parameter1:" and be left with just the "0xFFFF". If anyone can help and give a simple explanation of the operators used, that'd be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您需要 substr 函数。
Sounds like you need the substr function.
或
这将删除所有内容,直到并包括第一次出现的
:
后跟零个或多个空白字符。对于$s =~
,它会应用于$s
;如果没有它,它将应用于$_
。or
This deletes everything up to and including the first occurrence of
:
followed by zero or more whitespace characters. With$s =~
it's applied to$s
; without it, it's applied to$_
.您是否考虑过使用类似 Config::Std 的东西?
以下是如何手动解析这样的配置文件:
使用
Config::Std
:当然,在现实生活中,您可以将文件名传递给
read_config
吸食它。Have you considered using something like Config::Std?
Here is how to parse a configuration file like that by hand:
With
Config::Std
:Of course, in real life, you'd pass a file name to
read_config
instead of slurping it.我喜欢分割这些参数/值对。
请注意在拆分中使用 LIMIT,它将拆分限制为两个字段(以防值中存在其他冒号)。
I like split for these parameter/value pairs.
Note the use of LIMIT in the split, which limits the split to two fields (in case of additional colons in the value).