replace 相关问题

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

用从专用于找到的字母的数组中随机选择的字符串替换指定的字母

我有几封信;例如a、b、c、d 和e。 对于它们中的每一个,我都有一组两个字符的值,如下所示: a -> fg、dz、gc、bg b -> zt、hg、oq、vg、gb c -> lt、pr、cs、sh、pr...

回答 4 投票 0

PHP - 正常替换字符串并仅反转一次

每个单词只需替换一次,反之亦然。为此,我使用了不起作用的代码,并且找不到问题的答案。 输入: 你好 w1 w2 w12 新1 新12 新2 预计

回答 4 投票 0

在 Visual Studio Code 中搜索并替换为正则表达式以递增数字

我目前正在制作一个大的 svg 精灵。 不同的图像始终相距 2000 像素。 我所拥有的是: 我目前正在制作一个大型 svg 精灵。 不同的图像始终相距 2000px。 我拥有的是: <g transform="translate(0,0)"> <g transform="translate(0,2000)"> <g transform="translate(0,4000)"> 在正则表达式想要这个之后,只需将 2000 添加到第二个数字上: <g transform="translate(0,2000)"> <g transform="translate(0,4000)"> <g transform="translate(0,6000)"> 我现在遇到的问题是,一些新图像必须放在文档的顶部,这意味着我需要更改所有数字,而且它们非常多。 我正在考虑使用正则表达式,甚至发现它可以在 VS Code 的搜索栏中使用。问题是我从未使用过任何正则表达式,我有点困惑。 有人能给我一个将所有样本数增加 2000 的解决方案和解释吗? 我希望我之后能理解它,这样我就可以涉足这个话题了。 我也很高兴只提供一般教程或我的特定用例的链接。 非常感谢:) 在 VSCode 中,您无法在匹配/捕获中替换为递增值。您只能在作为替换参数传递给正则表达式替换函数/方法的回调函数内执行此操作。 安装Python脚本插件后,您可以使用Notepad++来执行这些替换。 按照这些说明进行操作,然后使用以下 Python 代码: def increment_after_openparen(match): return "{0}{1}".format(match.group(1),str(int(match.group(2))+2000)) editor.rereplace(r'(transform="translate\(\d+,\s*)(\d+)', increment_after_openparen) 请参阅 正则表达式演示。 注: (transform="translate\(\d+,\s*)(\d+) 匹配并捕获到第 1 组 transform="translate( + 1 个或多个数字,然后是 , 和 0 个或多个空格(使用 (transform="translate\(\d+,\s*))),然后将任何一个或多个数字捕获到第 2 组(使用 (\d+)) ) match.group(1) 是第 1 组内容,match.group(2) 是第 2 组内容。 基本上,任何组都是由一对未转义的括号组成的,并且组计数从 1 开始。因此,如果您使用像 (Item:\s*)(\d+)([.;]) 这样的模式,则需要使用 return "{0}{1}{2}".format(match.group(1),str(int(match.group(2))+2000), match.group(3))。或者,return "{}{}{}".format(match.group(1),str(int(match.group(2))+2000), match.group(3))。 我编写了一个扩展,查找和转换,使查找和替换正则表达式的数学运算变得非常简单(更像路径变量、条件、字符串操作等)。在这种情况下,这个键绑定(在您的keybindings.json中)将执行您想要的操作: { "key": "alt+r", // whatever keybinding you want "command": "findInCurrentFile", "args": { "find": "(?<=translate\\(\\d+,\\s*)(\\d+)", // double-escaped "replace": "$${ return $1 + 2000 }$$", "isRegex": true, // "restrictFind": "document", // or line/once/selections/etc. } } 如果您愿意,这也可以是您的 settings.json 中的设置 - 请参阅自述文件。 (?<=translate\\(\\d+,\\s*) 积极的lookbehind,您可以在lookbehind中使用非固定长度的项目,例如\\d+。 (\\d+) 捕获组 1 替换:$${ return $1 + 2000 }$$ $${ <your string or math operation here> }}$ return $1 + 2000 添加 2000 到捕获组 1 演示: SublimeText3 与 Text-Pastry 插件也可以做 \i 您可以使用我制作的扩展:Regex Text Generator 使用多光标选择数字,可以使用正则表达式查找和查找框中的Alt+Enter来完成 运行命令:根据正则表达式生成文本 作为Match Expression使用:(\d+) 作为发电机延伸使用:{{=N[1]+2000}} 您可以预览结果。 如果确定,请按 Enter,或按 Esc 中止 您可以在设置中将此类搜索替换设置为预定义 regexTextGen.predefined "regexTextGen.predefined": { "Add/Subtract a number" : { "originalTextRegex": "(\d+)", "generatorRegex": "{{=N[1]+1}}" } } 如果您选择预定义,则可以编辑表达式(更改 1)。

回答 4 投票 0

如何将字符串中的差异合并为一个

假设我有一个原始字符串,并且有两个其他字符串替换该字符串中的实例。例如: 我原来的字符串 A 是你好,你好吗? 那么,字符串 B 将是 !%$£*, ...

回答 1 投票 0

如何在Java中将字符串中的差异合并为一个

首先,很抱歉标题不好,因为我无法在不给出示例的情况下描述问题。 假设我有一个原始字符串,并且有两个其他字符串替换 t 中的实例...

回答 1 投票 0

使用自定义功能将表情符号短名称转换为图像

我创建了一个将表情符号短名称转换为图像的函数,但我不知道如何从字符串中获取表情符号短名称,以便该函数可以将它们转换为表情符号图像。 $field_from_db = ':

回答 1 投票 0

将记录转换为带有自定义分隔符的字符串

我可以通过执行 myrecord::text 将记录转换为字符串。我的问题是我的记录具有包含逗号的文本字段,并且生成的类型转换文本以逗号分隔。因此,我不能

回答 2 投票 0

查找行尾并将其替换为“ “逐字逐句

我希望使用一些文本编辑器来查找文档中的所有“行尾”,并用书面序列替换它们“ “(为了方便转换为另一种文件类型)。我一直在尝试

回答 5 投票 0

Delphi:从字符串中删除字符

我有一个包含字母、数字和其他字符的字符串。 我想从该字符串中删除所有数字、点和逗号 之前:“轴移动到新位置 - X-Pos:5.4mm / Y-Pos:3.5mm” 之后:'

回答 5 投票 0

用于在 vim 中搜索和替换的正则表达式

我有一个文件,其中包含多个条目,如下所示: Abcd:abcd:*一些文本*:klm:xyz/abc Abcd:abcd:R%fs90uw:klm:xyz/abc 现在,我想将“klm”替换为“qrs”,仅适用于那些具有“so...

回答 2 投票 0

使用 2 列作为“字典”来替换字符串中的字符

我有一个字符串,我需要替换其中的一些字符。 假设 1234AaCD 必须替换为 1Ą3Ęx•7D 现在,因为 char 被替换的规则很复杂,我有 2 列...

回答 2 投票 0

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

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