gpt4 book ai didi

regex - 使用正则表达式删除前导字符串

转载 作者:行者123 更新时间:2023-12-01 07:31:51 24 4
gpt4 key购买 nike

我需要构建一个 RegEx 以从给定的字符串中删除前导“The”或“A”或“An”和“空格”。

例如,给定的字符串是:

The quick brown fox jumps over the lazy dog



使用正则表达式,我希望删除领先的“The”并返回

quick brown fox jumps over the lazy dog



我试过了(从评论中添加)
^*(?<=[The|An|A]\s){1}.*

它工作正常,但在一种情况下它没有返回预期的结果。请看下面的场景。

Input: The quick brown fox --> Result = quick brown fox

Input: A quick brown fox --> Result = quick brown fox

Input: In A sunny day --> Result = A sunny day (expected is In a sunny day.. as the string is not starting with A)

Input: American An bank --> Result = An bank (expected is American An bank.. as the string is not starting with An)

最佳答案

你自己尝试过什么?您想要实现的目标并不困难,请尝试例如this tutorial on Regular-Expresions.info .

你想得太复杂了。尝试这个:

^(The|An|A)\s+

并替换为空字符串。

看到它 here on Regexr
^匹配字符串的开头。
(The|An|A)一个替代方案,匹配第一个合适的替代方案。
\s+匹配至少一个以下空格。

变化

The quick brown fox

A quick brown fox

In A sunny day

American An bank





quick brown fox

quick brown fox

In A sunny day

American An bank

关于regex - 使用正则表达式删除前导字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com