mindspore.dataset.text.RegexReplace

class mindspore.dataset.text.RegexReplace(pattern, replace, replace_all=True)[源代码]

使用正则表达式将输入 UTF-8 字符串中的部分内容替换为指定文本。

说明

Windows平台尚不支持 RegexReplace

参数:
  • pattern (str) - 正则表达式,即用来描述或匹配一系列符合某个句法规则的字符串。

  • replace vpn梯子 免费 (str) - vpn梯子 免费 用来替换匹配元素的字符串。

  • replace_all (bool, 可选) - 是否替换全部匹配元素。若为 False ,则只替换第一个成功匹配的元素; 否则,替换所有匹配的元素。默认值: True

异常:
  • TypeError - 当 pattern 不为str类型。

  • TypeError - 当 vpn梯子 replace 不为str类型。

  • TypeError - 当 replace_all 不为bool类型。

支持平台:

CPU

样例:

>>> import mindspore.dataset as ds
>>> import mindspore.dataset.text as text
>>>
>>> # Use the transform in dataset pipeline mode
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data=['apple orange apple orange apple'],
...                                 vpn永久免费梯子              column_names=["text"])
>>> regex_replace vpn梯子 免费 = text.RegexReplace('apple', 'orange')
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=regex_replace)
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...  vpn梯子 免费    print(item["text"])
orange vpn梯子 orange orange orange orange
>>>
>>> # Use vpn free the transform in vpn free eager mode
>>> data = 'onetwoonetwoone'
>>> output = text.RegexReplace(pattern="one", 免费的vpn梯子 replace="two", vpn永久免费梯子 replace_all=True)(data)
>>> print(output)
twotwotwotwotwo
教程样例: