【问题标题】:How can I replace the first occurrence of a character in every word?如何替换每个单词中第一次出现的字符?
【发布时间】:2020-06-08 19:49:28
【问题描述】:

如何替换每个单词中第一次出现的字符?

假设我有这个字符串:

hello @jon i am @@here or @@@there and want some@thing in '@here"
#     ^         ^^        ^^^                   ^          ^ 

我想删除每个单词上的第一个@,这样我最终会得到一个像这样的最终字符串:

hello jon i am @here or @@there and want something in 'here
#     ^        ^        ^^                   ^         ^

为了清楚起见,“@”字符总是一起出现在每个单词中,但可以出现在单词的开头或其他字符之间。

如果“@”字符仅出现一次,我设法通过使用我在Delete substring when it occurs once, but not when twice in a row in python 中找到的正则表达式的变体来删除它,它使用负前瞻和负后瞻:

@(?!@)(?<!@@)

查看输出:

>>> s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
>>> re.sub(r'@(?!@)(?<!@@)', '', s)
"hello jon i am @@here or @@@there and want something in 'here"

所以下一步是在“@”出现多次时替换它。这很容易通过s.replace('@@', '@') 将“@”从它再次出现的地方删除。

但是,我想知道:有没有办法一次性完成这个替换?

【问题讨论】:

  • 您需要严格的正则表达式答案吗?
  • @SayandipDutta 原则上是的,但我也很想看看没有正则表达式的其他方法:)
  • 只是为了确定,是否有类似 @Hello@There 的字符串,而 @ 不会是连续的?
  • @JvdV 不,不会有这种情况。

标签: python regex


【解决方案1】:
# Python3 program to remove the @ from String


def ExceptAtTheRate(string):
    # Split the String based on the space
    arrOfStr = string.split()

    # String to store the resultant String
    res = ""

    # Traverse the words and
    # remove the first @ From every word.
    for a in arrOfStr:
        if(a[0]=='@'):
            res += a[1:len(a)] + " "
        else:
            res += a[0:len(a)] + " "

    return res


# Driver code
string = "hello @jon i am @@here or @@@there and want some@thing in '@here"

print(ExceptAtTheRate(string))

输出:

【讨论】:

  • 谢谢!请注意,根据我的要求,还应删除 some@thing'@here 中的 @
【解决方案2】:

在生成器表达式中使用replace('@', '', 1) 怎么样?

string = 'hello @jon i am @@here or @@@there and want some@thing in "@here"'
result = ' '.join(s.replace('@', '', 1) for s in string.split(' '))

# output: hello jon i am @here or @@there and want something in "here"

1 的 int 值是可选的 count 参数。

str.replace(old, new[, count])

返回所有字符串的副本 子字符串 old 的出现被 new 替换。如果可选参数 count 已给出,仅替换前 count 次出现。

【讨论】:

  • 这是一个聪明的把戏!由于replace的第三个参数是replace(search, replace, max_matches),它只是替换每个单词的第一个。
  • @fedorqui'SOstopharming' 是的,它叫count,我添加了文档中的描述。
  • 注意这种副作用:如果您有多个空格 (' '),它们将丢失并被单个 ' ' 替换。
【解决方案3】:

DEMO

(?<!@)@

你可以试试这个。 见演示。

【讨论】:

    【解决方案4】:

    你可以像这样使用re.sub

    import re
    
    s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
    s = re.sub('@(\w)', r'\1', s)
    print(s)
    

    这将导致:

    "hello jon i am @here or @@there and want something in 'here"
    

    这是一个概念证明:

    >>> import re
    >>> s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
    >>> re.sub('@(\w)', r'\1', s)
    "hello jon i am @here or @@there and want something in 'here"
    >>> 
    

    【讨论】:

      【解决方案5】:

      正在考虑如果只有最后一个字符是 @ 并且您不想删除它,或者您有特定允许的起始字符的情况,想出了这个:

      >>> ' '.join([s_.replace('@', '', 1) if s_[0] in ["'", "@"] else s_ for s_ in s.split()])
      "hello jon i am @here or @@there and want some@thing in 'here"
      

      或者,假设您只想替换 @,前提是它位于前 n 个字符中

      >>> ' '.join([s_.replace('@', '', 1) if s_.find('@') in range(2) else s_ for s_ in s.split()])
      "hello jon i am @here or @@there and want some@thing in 'here"
      

      【讨论】:

        【解决方案6】:

        我会对以下模式进行正则表达式替换:

        @(@*)
        

        然后只需替换为第一个捕获组,它都是连续的@符号,减去一个。

        这应该捕获每个单词开头出现的每个@,无论是字符串开头、中间还是结尾的单词。

        inp = "hello @jon i am @@here or @@@there and want some@thing in '@here"
        out = re.sub(r"@(@*)", '\\1', inp)
        print(out)
        

        打印出来:

        hello jon i am @here or @@there and want something in 'here
        

        【讨论】:

          猜你喜欢
          • 2015-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-05
          • 1970-01-01
          • 1970-01-01
          • 2018-08-11
          • 2022-10-07
          相关资源
          最近更新 更多