在Python字符串中删除字符串中的'(data)’。

huangapple go评论71阅读模式
英文:

Remove in side '(data)' in string python

问题

我要翻译的内容:

在Python中从字符串中删除'(data)'中的内容,我有一个字符串如下:"i am learning python programming, (Because) it is easy langue for new learner(s)",我只想删除第一个(data)数据。在(data)中可能包含任何内容,不是固定的。

英文:

Remove in side '(data)' in string python, I have string like this "i am learning python programming, (Because) it is easy langue for new learner(s)" i want to remove first (Because) data only. In side (data) may vary anything. Not constant.

答案1

得分: 0

你可以使用正则表达式:

import re
string = "我正在学习Python编程,因为它对新学习者来说很容易。"
''.join(re.split(r"\([^)]*\)", string))
'我正在学习Python编程,因为它对新学习者来说很容易。'

这个模式搜索开括号,然后是除了闭括号以外的任何内容,然后是闭括号。

英文:

You can use regex:

>>> import re
>>> string = "i am learning python programming, (Because) it is easy langue for new learner(s)"
>>> ''.join(re.split(r"\([^)]*\)", string))
'i am learning python programming,  it is easy langue for new learner'

The pattern searches for opening brace, then any stuff except closing brace, then closing brace.

huangapple
  • 本文由 发表于 2020年1月3日 19:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定