如何使用 REGEX 模式删除特定单词“THE”仅当在文本字符串的开头时?

发布于 2024-09-27 23:02:25 字数 260 浏览 4 评论 0原文

我有一个用于各种内容标题的文本输入字段,并帮助最大限度地减少搜索结果的误报(内部搜索不是最好的),我需要一个 REGEX 模式来查看输入字符串的前四个字符并删除单词(以及单词后面的空格)_the _(如果它仅出现在开头)。

例如,如果我们正在谈论乐队的名称,并且有人输入 The Rolling Stones ,我需要的是条目仅显示 Rolling Stones

Can a regex be use a regex自动删除这4个字符?

I have a text input field for titles of various things and to help minimize false negatives on search results(internal search is not the best), I need to have a REGEX pattern which looks at the first four characters of the input string and removes the word(and space after the word) _the _ if it is there at the beginning only.

For example if we are talking about the names of bands, and someone enters The Rolling Stones , what i need is for the entry to say only Rolling Stones

Can a regex be used to automatically strip these 4characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-10-04 23:02:25

应用正则表达式

^(?:\s*the\s*)?(.*)$

将匹配任何字符串,并在反向引用号中捕获它。 1,除非它以 the 开头(可以选择用空格包围),在这种情况下 backref no。 1 将包含以下内容。

您需要在正则表达式引擎中设置不区分大小写的选项才能使其工作。

Applying the regex

^(?:\s*the\s*)?(.*)$

will match any string, and capture it in backreference no. 1, unless it starts with the (optionally surrounded by whitespace), in which case backref no. 1 will contain whatever follows.

You need to set the case-insensitive option in your regex engine for this to work.

愿与i 2024-10-04 23:02:25

您可以使用 ^ 标识符来匹配行开头的模式,但是对于您使用它的目的来说,它可能被认为是多余的。

很多语言都支持字符串操作,这是一个更合适的选择。我可以提供一个用Python来演示的例子,

>>> def func(n):
    n = n[4:len(n)] if n[0:4] == "The " else n  
    return n

>>> func("The Rolling Stones")
'Rolling Stones'
>>> func("They Might Be Giants")
'They Might Be Giants'

You can use the ^ identifier to match a pattern at the beginning of a line, however for what you are using this for, it can be considered overkill.

A lot of languages support string manipulations, which is a more suitable choice. I can provide an example to demonstrate in Python,

>>> def func(n):
    n = n[4:len(n)] if n[0:4] == "The " else n  
    return n

>>> func("The Rolling Stones")
'Rolling Stones'
>>> func("They Might Be Giants")
'They Might Be Giants'
难得心□动 2024-10-04 23:02:25

由于您没有用语言澄清,这里有一个 Perl 解决方案:

my $str = "The Rolling Stones";

$str =~ s/^the //i;

say $str; # Rolling Stones

As you don't clarify with language, here is a solution in Perl :

my $str = "The Rolling Stones";

$str =~ s/^the //i;

say $str; # Rolling Stones
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文