简体   繁体   English

可以匹配并返回一部分文本的正则表达式,同时删除 rest 以使性别特定文本成为可能

[英]Regex that can match and return a part of text while remove the rest to make gender specific text possible

Don't know what to try.不知道该尝试什么。 I keep getting these closed, because I'm told it is not explained, not specific, whatever.我一直关闭这些,因为有人告诉我它没有解释,不具体,无论如何。 Perhaps rather than shutting this down instantly, someone on this site can be kind enough to help me?也许不是立即关闭它,这个网站上的人可以帮助我吗? Is that possible without shutting down the thread instantly and before anyone can even ask a question?这有可能不立即关闭线程并且在任何人甚至可以提出问题之前关闭线程吗?

In Javascript (Google App Scripts).在 Javascript(Google App 脚本)中。

String="I want [him/her] to go to [his/her] side when [he/she] is done." String="当 [他/她] 完成后,我希望 [他/她] 到 go 到 [他/她] 身边。"

What is the regex that I'm trying to figure out would allow the result to be: "I want him to go to his side when he is done."我试图弄清楚的正则表达式会允许结果是:“我希望他完成后将 go 带到他身边。”

The common factor will be [.../...]共同因素将是 [.../...]

Since the second half of this question appears too be confusing and I keep getting the question closed before anyone has a chance to comment....I'll ask almost the same exact question again in a second post.由于这个问题的后半部分看起来太混乱了,我一直在任何人有机会发表评论之前关闭这个问题……我将在第二篇文章中再次提出几乎完全相同的问题。 You might guess what my follow up question will be, but apparently that is not appropriate to ask...I'll give extra brownie points if you know what I'm going to ask and can answer it.您可能会猜到我的后续问题是什么,但显然这样问是不合适的……如果您知道我要问什么并且可以回答,我会加分。

The pattern, as represented in a RegExp is:正则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 ]  = ]

So putting this all together, with () for capture groups and adding a g flag for a global search, we get this mouthful since the string pattern we're looking for is rife with characters that are special to RegExp:因此,将所有这些放在一起,使用()捕获组并添加g标志以进行全局搜索,我们得到了这一口,因为我们正在寻找的字符串模式充满了 RegExp 特有的字符:

const pronounPattern = /\[([^\/]*)\/([^\]]*)]/g;

Now when we do a match/replace, because we have capture groups we get a pseudo-array, with index 0 being the full match, 1 being option A, and 2 being option B. This lets us now do a string replace.现在当我们进行匹配/替换时,因为我们有捕获组,我们得到一个伪数组,索引 0 是完全匹配,1 是选项 A,2 是选项 B。这让我们现在可以进行字符串替换。 For example, if we always want to use option A, you can do:例如,如果我们总是想使用选项 A,您可以这样做:

const replaced = yourString.replace(pronounPattern, '$1');

The $1 means "use capture group 1" which is in our case the first in the either/or group. $1表示“使用捕获组 1”,在我们的例子中是 either/or 组中的第一个。 Use $2 for the second option.使用$2作为第二个选项。 Putting this together in a function:把它放在一个 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");

Hope it helps.希望能帮助到你。

not javascript but a regex solution with sed不是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.

you need to escape square brackets in literal use since it's a special regex char.你需要在字面使用中转义方括号,因为它是一个特殊的正则表达式字符。

easy to change to get the second alternative易于更改以获得第二个选择

 ...  | 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM