如何使用正则表达式去除字符串中多余的空格 [英] How to Remove Excess Whitespace in String Using Regex

查看:51
本文介绍了如何使用正则表达式去除字符串中多余的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用正则表达式解析一系列段落,但不幸的是,该段落在句子之间出现了许多空格,有时还有单词.我希望能够删除所有多余的空白,但我不确定如何......有人有任何想法吗?我不想删除所有空格,这是我迄今为止发现的唯一内容,但要保持常规段落格式,例如在每个单词之后都有一个空格,并且每个标点+单词之后都有一个空格.我正在用 Perl 编码.

I have a series of paragraphs that I want to parse using regular expressions, but unfortunately, the paragraph is appearing with many white spaces in between sentences, and sometimes words. I would like to be able to remove all excess white space, but I'm unsure how... Anyone have any ideas? I don't want to remove all whitespace, which is the only thing I've found so far, but to keep regular paragraph format, as in after every word have a white space, and after every punctuation+word have a whitespace. I am coding in Perl.

任何帮助将不胜感激!

推荐答案

规范化水平空白:

s/\h+/ /g;

规范化垂直空白:

s/\v+/\n/g;

规范化所有空格:

s/[\h\v]+/ /g;

这篇关于如何使用正则表达式去除字符串中多余的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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