Regex with unusual characters

Regex with unusual characters

噩梦成真你也成魔 发布于 2021-11-25 字数 341 浏览 921 回复 4 原文

I'm trying to come up with some regex to handle the # sign.

Example, #PRODUCT_143#

If the input were #PRODUCT_143, the regex #PRODUCT_(d*$) matches and returns 143 as the match. But adding the # to the end of both the input and the regex causes it to break. what do I need to do here to get this to match?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

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

评论(4

热血少△年 2022-06-07 4 楼

The $ probably matches as the end-of-line, so your extra # is confusing it:

#PRODUCT_(d*)#$
我偏爱纯白色 2022-06-07 3 楼

Did you just forget to move the $ out of the parenthesis?

    ^#PRODUCT_([0-9]*)#$

Note the added ^ - may be it is not right for your input.

〃温暖了心ぐ 2022-06-07 2 楼

It shouldn't break anything, maybe you've forgotten to move the $ in the regex, which means end of line? This regex should be fine:

#PRODUCT_(d*)#$

(I'm not sure why you were capturing the end of line in your original regex).

吾性傲以野 2022-06-07 1 楼

If you tried #PRODUCT_(d*$)# it’s no surprise that it didn’t found a match. Since the $ already marks the end of the string and the # after it will never be matched.

So try this instead:

#PRODUCT_(d*)#$