如何使用 REGEX 模式删除特定单词“THE”仅当在文本字符串的开头时?
我有一个用于各种内容标题的文本输入字段,并帮助最大限度地减少搜索结果的误报(内部搜索不是最好的),我需要一个 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应用正则表达式
将匹配任何字符串,并在反向引用号中捕获它。 1,除非它以
the
开头(可以选择用空格包围),在这种情况下 backref no。 1 将包含以下内容。您需要在正则表达式引擎中设置不区分大小写的选项才能使其工作。
Applying the regex
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.
您可以使用
^
标识符来匹配行开头的模式,但是对于您使用它的目的来说,它可能被认为是多余的。很多语言都支持字符串操作,这是一个更合适的选择。我可以提供一个用Python来演示的例子,
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,
由于您没有用语言澄清,这里有一个 Perl 解决方案:
As you don't clarify with language, here is a solution in Perl :