最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

REGEX fails when adding an optional mark to a group - Stack Overflow

programmeradmin4浏览0评论

In the following string blah blahTESTblab la14blah-15S rebla I'm trying to capture TEST and 15S if present.

I wrote the following regex : (TEST).*?(\d{1,2}S)?

Everything is going well until the last ?. The last group really needs to be optional. And groups can't be anchored, as strings I'm searching in are human written, ie tot reliable.

What's wrong ?

In the following string blah blahTESTblab la14blah-15S rebla I'm trying to capture TEST and 15S if present.

I wrote the following regex : (TEST).*?(\d{1,2}S)?

Everything is going well until the last ?. The last group really needs to be optional. And groups can't be anchored, as strings I'm searching in are human written, ie tot reliable.

What's wrong ?

Share Improve this question asked Mar 18 at 14:02 user9428517user9428517 234 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The following pattern seems to do what you want:

(TEST)(?:.*?(\d{1,2}S)|)

This says to match:

  • (TEST) a required "TEST" in the first capture group
  • (?: do not capture
    • .*?(\d{1,2}S) match either any string ending in e.g. "15S" in the second capture group
    • | OR
    • (match nothing at all beyond the first "TEST")
  • ) end non capture group

Demo

Thanks again for this answer. I modified your regex a bit in order to work correctly with the libreoffice regex function. I added .* twice, at the beginning and at the end of the regex.

With blah blahTESTblab la14blah-15S reblain cell A1 :

  • =REGEX(A1;"(?i)(TEST)(?:.*(\d{1,2}S)|)";"★$1$2") gives blah blah★TEST15S rebla
  • =REGEX(A1;"(?i).*(TEST)(?:.*(\d{1,2}S)|).*";"★$1$2") gives ★TEST15S
发布评论

评论列表(0)

  1. 暂无评论