replace 相关问题

替换是搜索字符串以查找子字符串并将其替换为其他字符串的操作。

Pandas 数据框中 str.replace() 的奇怪行为。删除与目标字符串不匹配的值

我有一个天气数据的数据框,我正在从 csv 文件中读取该数据框,其中两列“Sea_Level_Pressure”和“Wind_Speed”的数值带有后缀“s”,我想

回答 2 投票 0

Regex(正则表达式),替换javascript中第二次出现的地方

这是正在使用的字符串的示例: xxxxxx[xxxxxx][7][xxxxxx][9][xxxxxx] 我在匹配第二次出现的匹配时遇到了一些麻烦,我想返回第二个 sq...

回答 3 投票 0

创建可以从表中查询的 Redshift UDF

我需要将输入字符串替换为 Redshift 中表中的值 我在 SQL 中有这个函数,它用表中的值替换输入字符串。我无法让它在 Redshift 中工作,因为它

回答 1 投票 0

根据 2 列 Excel 电子表格编写 AppleScript 来查找和替换 Excel 电子表格/Word 文档中的文本?

我有一个 Excel 电子表格大师。 A 列列出单词和短语; B 列列出了它们的缩写。 我想编写一个 AppleScript 来在 Word 文档中查找并用 ColumnB 1) 替换 ColumnA...

回答 1 投票 0

使用javaScript替换字符串中的字母

函数 dnaStrand(dna){ const compl = DNA; dna.includes("A") && compl.replace(/A/g, "T"); dna.includes("T") && compl.replace(/T/g, "A") ...

回答 2 投票 0

使用 Python 删除字符串中的 HTML 标签的最佳方法是什么?

我想知道使用 python 清除该字符串中所有 HTML 标签的最有效方法是什么。 Lorem ipsum dolor sat amet,consectetur adipiscing 伊莱... 我想知道使用 python 清除该字符串中所有 HTML 标签的最有效方法是什么。 <p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing elit</strong></p>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </br> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <a href="">Excepteur sint occaecat</a> cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 我尝试使用replace('[insert tag]',''),但我必须创建多个replace()行来删除所有标签。 从字符串中删除 HTML 标签的一种方法是使用 Beautiful Soup 库。调用 text() 函数会提取所有文本并删除所有标签。 BeautifulSoup 将仅解析和删除 HTML 标签,而不仅仅是删除以“<' and ending with a '>”开头的任何文本序列。它还会自动翻译 HTML 实体;例如&lt; => '<', &gt; => '>' 等 from bs4 import BeautifulSoup html = """<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing elit</strong></p>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </br> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <a href="">Excepteur sint occaecat</a> cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>""" soup = BeautifulSoup(html, "html.parser") print(soup.text) 输出: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 你可以试试 import re def remove_html_tags(text): clean = re.compile('<.*?>') return re.sub(clean, '', text) 有多种方法可以从 Python 中的字符串中删除 HTML 标签。 希望有帮助 正则表达式 最简单的方法是使用正则表达式删除标签。为此,您需要首先import re。然后你想删除<>里面的所有东西。为此,您可以使用 remover = re.compile('<.*?>') 完整的代码应该如下所示: import re string = '''<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing elit</strong></p>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </br> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <a href="">Excepteur sint occaecat</a> cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>''' remover = re.compile('<.*?>') print(re.sub(remover, '', string)) LXML 另一种方法是使用 lxml 库,可以通过 pip install lxml 安装该库。 lxml 模块有一个内置函数,可以从 html 中删除所有标签。 代码看起来像这样: from lxml import html string = '''<p>Lorem ipsum dolor sit amet, <strong>consectetur adipiscing elit</strong></p>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</br> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </br> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <a href="">Excepteur sint occaecat</a> cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>''' print(html.fromstring(string).text_content()) 代码 html.fromstring(string).text_content() 是将 html 转换为文本,从而从中删除所有标签。 如果您想要单行删除标签而不需要re模块。例如在 Python 2.7 中使用 expr。试试这个: heading = 'hello<br>\n<br/>there' ' '.join([list(reversed(s.strip().split('>')))[0] for s in heading.split('<') if not s.strip().endswith('>')]) >>> 'hello there'

回答 4 投票 0

如何交换字符串 JavaScript 中的字符位置

我正在制作一个解密函数,但我被困在需要交换字符串的第二个字母和最后一个字母的位置的部分。 我也尝试过使用替换方法,但我...

回答 6 投票 0

引用列表来删除字符串值

以下 def clean_sheet_title 函数引用 INVALID_TITLE_CHAR 和 INVALID_TITLE_CHAR_MAP 来去除无效字符并将标题限制为 31 个字符 - # 这会剥离字符...

回答 2 投票 0

通过分组str_replace_all

我需要用长名称替换缩写名称。 但是,我在不应该更换的情况下得到了错误的行为。这种替换应该尊重国家的情况。 我在用着:

回答 1 投票 0

在Word VBA中设置特定条件替换文本

我正在尝试创建一个宏,用特定条件替换一段文本。 例如: 如果它显示在文档“Summary nº 123”中,则保持文本原样, 但如果我...

回答 1 投票 0

R:如果不适用,则根据条件并使用列名称模式,将缺失值替换为另一列中的缺失值

我遇到以下问题:对于一项评级,6 个数字列中的 3 个数字列中的值丢失。 我希望将缺失的值替换为所在单元格中的值...

回答 1 投票 0

无法在.NET Core C# 中反序列化 API 响应 json

我有第 3 方 Web api,它返回 json 响应,其中包含转义字符和许多反斜杠,如下所示。 我的目标是阅读所有服务组及其相关服务。我什至无法忍受

回答 1 投票 0

使用 sed 替换大括号之间的模式,其中左大括号之前的行有一个已知单词

我想改变: 进口 { 类型补丁; ... } 出口 { 类型补丁; } 到 进口 { 类型墙; ... } 出口 { 类型补丁; } 我尝试使用...

回答 1 投票 0

如何自动将ast-grep重写规则应用于文件?

我正在使用ast-grep,一个用于代码结构搜索和重写的CLI工具,来查找和替换代码中的一些模式。我已经编写了一个带有一些重写规则的 YAML 文件,我可以使用 sg scan 来...

回答 1 投票 0

powershell - 仅用目标目录中的新文件替换旧文件

大家好, 我只想用新文件替换旧文件 我试过 设置位置C

回答 0 投票 0

仅替换 VBA 中字符串中最后一次出现的匹配项

我有一个像这样的字符串 “C://Documents/TestUser/WWW/Help/Files/Move_Help.txt” 并且必须将 Move_Help.txt 替换为 Move_Job.txt 我在 VBA EXCEL 中使用以下代码 str =“C://文档/

回答 4 投票 0

替换文档中部分单词的有效方法,其中替换单词在 Excel 中定义

我有一个很大的.txt 文件 我已经在 python 中使用 readlines() 阅读了它 样本: 行= [“XX_A_Name,A_Bad_Joke ='67', ”,“一只快速黑杰克狐狸 JJ_Value 超过 XX_A_Name a.la...

回答 1 投票 0

将缩进字符串转换为括号

我有一个带有缩进文本的变量。我需要做的就是将该字符串转换为带括号的字符串。示例代码如下: def indent_to_bracket(文本): ... 返回...

回答 3 投票 0

从字符串中删除整个黑名单单词

下面的代码的问题在于它从字符串中删除字符而不是单词。 $str = "一分钟内,拔掉地窖里这些瓶子的所有软木塞"; $

回答 3 投票 0

PHP 从字符串中删除单词(不是字符)

下面的代码的问题在于它从字符串中删除字符而不是单词。 下面的代码的问题在于它从字符串中删除了 characters 而不是 words。 <?php $str = "In a minute, remove all of the corks from these bottles in the cellar"; $useless_words = array("the", "of", "or", "in", "a"); $newstr = str_replace($useless_words, "", $str); //OUTPUT OF ABOVE: "In mute, remove ll cks from se bottles cellr" ?> 我需要的输出是:分钟,从这些酒窖中取出所有软木塞 我假设我无法使用str_replace()。我可以做什么来实现这个目标? . preg_replace将完成这项工作: $str = "The game start in a minute, remove all of the corks from these bottles in the cellar"; $useless_words = array("the", "of", "or", "in", "a"); $pattern = '/\h+(?:' . implode($useless_words, '|') . ')\b/i'; $newstr = preg_replace($pattern, "", $str); echo $newstr,"\n"; 输出: The game start minute, remove all corks from these bottles cellar 说明: 图案看起来像:/\h+(?:the|of|or|in|a)\b/i / : regex delimiter \h+ : 1 or more horizontal spaces (?: : start non capture group the|of|or|in|a : alternatives for all the useless words ) : end group \b : word boundary, make sure we don't have a word character before /i : regex delimiter, case insensitive $useless_words = array(" the ", " of ", " or ", " in ", " a "); $str = "In a minute, remove all of the corks from these bottles in the cellar"; $newstr = str_replace($useless_words, " ", $str); $trimmed_useless_words = array_map('trim',$useless_words); $newstr2 = ''; foreach ($trimmed_useless_words as &$value) { if (strcmp($value, substr($newstr,0,strlen($value)))){ $newstr2 = substr($newstr, strlen($value) ); break; } } if ($newstr2 == ''){ $newstr2 = $newstr; } echo $newstr2; 除了强制整个单词与单词边界匹配之外,还可以使用条件表达式来消耗前导或尾随空白字符(但不能同时使用两者!)。 代码:(演示) $str = "The game start in a minute, remove all of the corks from these bottles in the cellar they're in"; $useless_words = ["the", "of", "or", "in", "a"]; var_dump( preg_replace('/(\s+)?\b(?:' . implode('|', $useless_words) . ')\b(?(1)|\s+)/i', '', $str) ); 输出: string(69) "game start minute, remove all corks from these bottles cellar they're" 请注意,前导 The 加上尾随空格被删除,最后一个 in 及其前导空格被删除,all of the corks 变成了 all corks,仅由一个空格分隔。

回答 3 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.