gpt4 book ai didi

正则表达式匹配两个之间没有空格的字符串 #

转载 作者:行者123 更新时间:2023-12-04 03:54:51 25 4
gpt4 key购买 nike

我正在寻找一个正则表达式,它返回两个主题标签之间的字符串,但前提是两者之间 # 没有空格。

我当前的正则表达式:\#(.*?)\#

测试用例 1:

'#thisshouldbeparsed# #2 test'
=> thisshouldbeparsed (this is currently correct)

测试用例 2:

'test #2 #thisshouldbeparsed#'

=>2 (this is not correct)

我需要一个正则表达式,它也返回 "thisshouldbeparsed"

最佳答案

你可以使用

#([^#\s]*)#

为避免出现空匹配,请将 * 替换为 +:

#([^#\s]+)#

参见 regex demo .

详情

  • # - # 字符
  • ([^#\s]*) - 第 1 组:# 和空格以外的任何零个或多个字符
  • # - # 字符。

关于正则表达式匹配两个之间没有空格的字符串 #,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64063622/

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