- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 stackoverflow 上发现了一些类似的主题,但我是 Python 和 Reg Exps 的新手。
我有一个字符串
,"Completely renovated in 2009, the 2-star Superior Hotel Ibis Berlin Messe, with its 168 air-conditioned rooms, is located right next to Berlin's ICC and exhibition center. All rooms have Wi-Fi, and you can surf the Internet free of charge at two iPoint-PCs in the lobby. We provide a 24-hour bar, snacks and reception service. Enjoy our breakfast buffet from 4am to 12pm on the 8th floor, where you have a fantastic view across Berlin. You will find free car parking directly next to the hotel.",
模式应该是这样的:comma, double quote|any text with commas |double quote, comma
。我需要用双引号替换逗号,例如 @
字符。我应该使用哪种正则表达式模式?
我试过这个:
r',"([.*]*,[.*]*)*",'
有不同的变体,但它不起作用。
感谢解答,问题已解决。
最佳答案
如果您需要做的只是用 @ 字符替换逗号,您应该考虑使用 str_replace
而不是正则表达式。
str_a = "Completely renovated in 2009, the 2-star Superior Hotel Ibis Berlin Messe, with its 168 air-conditioned rooms, is located right next to Berlin's ICC and exhibition center. All rooms have Wi-Fi, and you can surf the Internet free of charge at two iPoint-PCs in the lobby. We provide a 24-hour bar, snacks and reception service. Enjoy our breakfast buffet from 4am to 12pm on the 8th floor, where you have a fantastic view across Berlin. You will find free car parking directly next to the hotel."
str_a = str_a.replace('","', '@') #commas inside double quotes
str_a = str_a.replace(',', '@') #replace just commas
print str_a
编辑:或者,您可以列出要替换的内容,然后遍历它并执行替换。例如:
to_replace = ['""', ',', '"']
str_a = "Completely renovated in 2009, the 2-star Superior Hotel Ibis Berlin Messe, with its 168 air-conditioned rooms, is located right next to Berlin's ICC and exhibition center. All rooms have Wi-Fi, and you can surf the Internet free of charge at two iPoint-PCs in the lobby. We provide a 24-hour bar, snacks and reception service. Enjoy our breakfast buffet from 4am to 12pm on the 8th floor, where you have a fantastic view across Berlin. You will find free car parking directly next to the hotel."
for a in to_replace:
str_a = str_a.replace(a, '@')
print str_a
关于python - 去除字符串中的逗号,用逗号和双引号包围/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18235923/
我想在页面中放置这样一个栏: [ Angular -l][变量][装饰][变量][ Angular -r] “可变”间隔部分应该可以调整大小,但最终,周围的容器(包含所有这些)应该规定最大宽度,并且上
我想写一个正则表达式,这样我只匹配没有用方括号括起来的第一个数字。 例如asdadsas,*&(*&(*2asdasd*U(*&*()&(*3应该匹配 2(没有方括号) 和 asdadsas,*&(*
我尝试尝试这些 iostream 奇怪的行为。 int value = 2; cout > 2) > 的性质。在该语言从 C 发展的早期,这些运算符专门用作所谓的“位移位”运算符,其中 > 会将位向右
这个问题在这里已经有了答案: How to use Objective-C classes with names which are keywords in Swift (1 个回答) 关闭 7 年
我看不到问题,我没有对参数进行编码,我只是将变量从一个页面传递到另一个页面。 这是页面 A: if (isset($_SESSION['loggedIn'])==1){ ?> '">Edit 这
我有一个静态工厂类,如果在调用之前没有进行初始化,它会抛出异常。我不明白为什么 Eclipse 对我大喊大叫,因为我在工厂中抛出了异常(它要求我在 try/catch 中包围抛出异常),但在另一个类中
我有一些代码: public static void a() throws NumberFormatException { return; } public stati
我看不到问题,我没有对参数进行编码,我只是将变量从一个页面传递到另一个页面。 这是页面 A: if (isset($_SESSION['loggedIn'])==1){ ?> '">Edit 这
This question already has answers here: Using explicitly numbered repetition instead of question mar
包围
在我的网站上,我有一些图像与其余内容存储在相同的 SQL 单元格中。当我加载包含图像的页面并且 PHP 回显“文章”单元格时,图像会自动包含在段落标签中,这不是我想要的。有什么办法可以防止这种情况发生
我想弄清楚为什么我的 JSON 响应被方括号括起来。我正在使用 ASP.NET Web API 和 Angular。我在想这就是我的 Angular 代码没有打印到 HTML 的原因。 namespa
在eclipse中我们可以用try/catch包围一段代码。我想用 if 语句包围一段代码。有没有快捷键。选择代码块后按 Ctrl + 1 没有提示用 If 包围。 最佳答案 突出显示代码块,按 Al
我有一个用 Go 实现的 Web 服务,它从外部服务返回一个 JSON 结构。返回对象后,它看起来像这样: {"otherServiceInfoList":[],"action... 我的 Go 网络
我在一个网站上工作,客户想要类似这样的东西:http://www.csszengarden.com/?cssfile=202/202.css有几个叠加层附加到屏幕的边缘,而中心的文本以原始浏览器滚动条
我想圈出 react-native-vector 图标。我在样式中添加了一个边框半径,但它对所有设备都没有帮助,而且每个图标的行为都不同。 react 原生矢量图标的链接: https://
使用文本节点的以下值... MatcH one MatcHer two MarcH three 如何使用 java matcher.find() 创建以下输出? MatcH one MatcHer t
我有一个读取文件的非常简单的程序。 eclipse 要求我添加 throws 声明或用 try catch 包围。哪一个是“合适的”。以及如果我已经在方法级别 try catch ,为什么还必须拥有其
在我们从“psd2html”服务获得的代码中,我看到围绕 div 标签的内容有很多跨度。 我知道 span 和 div 之间的区别,但我不明白为什么代码看起来像这样: Forgot pass
我有一个 HTML 代码,有 3 个输入字段:
我正在尝试替换字符串中单词 true 的所有实例,例如在 "true && 0 || 1" 中. 这些字符串可能包含变量值,标记为#{varname} .我不想替换被 #{ 包围的实例和 } . 例子
我是一名优秀的程序员,十分优秀!