[英]Regex that can match and return a part of text while remove the rest to make gender specific text possible
不知道该尝试什么。 我一直关闭这些,因为有人告诉我它没有解释,不具体,无论如何。 也许不是立即关闭它,这个网站上的人可以帮助我吗? 这有可能不立即关闭线程并且在任何人甚至可以提出问题之前关闭线程吗?
在 Javascript(Google App 脚本)中。
String="当 [他/她] 完成后,我希望 [他/她] 到 go 到 [他/她] 身边。"
我试图弄清楚的正则表达式会允许结果是:“我希望他完成后将 go 带到他身边。”
共同因素将是 [.../...]
由于这个问题的后半部分看起来太混乱了,我一直在任何人有机会发表评论之前关闭这个问题……我将在第二篇文章中再次提出几乎完全相同的问题。 您可能会猜到我的后续问题是什么,但显然这样问是不合适的……如果您知道我要问什么并且可以回答,我会加分。
正则RegExp
中表示的模式是:
Starting [ = \[ (escaped since `[` has special meaning)
Option A = [^\/]* (any character except slash, repeated 0-to-many times)
Slash = \/ (escaped as above to differentiate from a RegExp literal terminator)
Option B = [^\]]* (any character except a `]`, repeated 0-to-many times)
Closing ] = ]
因此,将所有这些放在一起,使用()
捕获组并添加g
标志以进行全局搜索,我们得到了这一口,因为我们正在寻找的字符串模式充满了 RegExp 特有的字符:
const pronounPattern = /\[([^\/]*)\/([^\]]*)]/g;
现在当我们进行匹配/替换时,因为我们有捕获组,我们得到一个伪数组,索引 0 是完全匹配,1 是选项 A,2 是选项 B。这让我们现在可以进行字符串替换。 例如,如果我们总是想使用选项 A,您可以这样做:
const replaced = yourString.replace(pronounPattern, '$1');
$1
表示“使用捕获组 1”,在我们的例子中是 either/or 组中的第一个。 使用$2
作为第二个选项。 把它放在一个 function 中:
const MALE = 1;
const FEMALE = 2;
const pronounPattern = /\[([^\/]*)\/([^\]]*)]/g;
function replacePronounPlaceholders(str, option) {
return str.replace(pronounPattern, '$' + option);
}
// Used like:
const replaced = replacePronounPlaceholders(
"I want [him/her] to go to [his/her] side when [he/she] is done.",
FEMALE);
var myStr = 'I want [him/her] to go to [his/her] side when [he/she] is done.';
var newStr = myStr.replace(/\[him\/her\]/g, "him").replace(/\[his\/her\]/g, "his").replace(/\[he\/she\]/g,"he");
希望能帮助到你。
不是javascript
而是sed
的正则表达式解决方案
$ echo "I want [him/her] to go to [his/her] side when [he/she] is done." |
sed -E 's_\[([^/]+)/[^]]+\]_\1_g'
I want him to go to his side when he is done.
你需要在字面使用中转义方括号,因为它是一个特殊的正则表达式字符。
易于更改以获得第二个选择
... | sed -E 's_\[([^/]+)/([^]]+)\]_\2_g'
I want her to go to her side when she is done.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.