Main Content

whitespacePattern

匹配空白字符

自 R2020b 起

说明

pat = whitespacePattern 创建一个模式,该模式匹配由一个或多个空白字符(如空格和制表符)组成的文本。

示例

pat = whitespacePattern(N) 精确匹配由 N 个空白字符组成的文本。

示例

pat = whitespacePattern(minCharacters,maxCharacters) 匹配由大于或等于 minCharacters 个且小于或等于 maxCharacters 个空白字符组成的文本。infmaxCharacters 的有效值。whitespacePattern贪婪模式,匹配的空白字符数量尽可能接近 maxCharacters 个。

示例

全部折叠

使用 whitespacePattern 匹配非标准空白字符,如 char(160)

创建一个字符向量元胞数组,其中每个字符向量包含一个不同的空白字符,包括制表符和 newline 字符。

whitespaces = {' ' char(9) newline char(32) char(160)}
whitespaces = 1×5 cell
    {' '}    {'→'}    {'↵'}    {' '}    {' '}

使用 whitespacePattern 构建一个匹配空白字符的模式。使用 contains 确定哪些字符向量包含有效空白字符。

pat = whitespacePattern;
contains(whitespaces,pat)
ans = 1×5 logical array

   1   1   1   1   1

使用 whitespacePattern 将非标准空白替换为标准 ' ' 字符。

txt 创建为一个字符向量。

txt = ['This' char(9) 'char' newline 'vector' char(160) 'has' char(32) 'nonstandard' char(8193) 'spaces']
txt = 
    'This	char
     vector has nonstandard spaces'

使用 whitespacePatternpat 创建为一个匹配单个空白字符的模式对象。用单个空白替换匹配的文本部分。

pat = whitespacePattern(1);
txt = replace(txt,pat," ")
txt = 
'This char vector has nonstandard spaces'

当存在多个空白字符时,使用 whitespacePattern 来更正间距。

创建字符串 txt 变量。使用 whitespacePatternpat 创建为一个匹配两个或多个空白字符的模式对象。用单个空白替换匹配的文本部分。

txt = "Text looks   strange    with    extra    spaces";
pat = whitespacePattern(2,inf);
txt = replace(txt,pat," ")
txt = 
"Text looks strange with extra spaces"

输入参数

全部折叠

要匹配的字符数,指定为非负整数标量。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

要匹配的最小字符数,指定为非负整数标量。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

要匹配的最大字符数,指定为非负整数标量。

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

输出参量

全部折叠

模式表达式,以 pattern 对象形式返回。

详细信息

全部折叠

定义

空白是表示水平或垂直间距的任一字符或一系列字符。在呈现时,空白字符不对应于可见标记,但通常会占据页面上的某个区域。常见的空白字符包括:

实义空白字符

描述

char(32)

标准空白字符 ' '

char(133)

下一行

char(160)

不间断空格

char(8199)

图窗空格

char(8239)

不间断窄空格

有关详细信息,请参阅空白字符

扩展功能

基于线程的环境
使用 MATLAB® backgroundPool 在后台运行代码或使用 Parallel Computing Toolbox™ ThreadPool 加快代码运行速度。

版本历史记录

在 R2020b 中推出