首页 > 解决方案 > End a regular expression pattern with a string

问题描述

all. I have spent some time now to learn regular expression, but eventually there is a problem I cannot solve properly.

Lets assume the following 'string' (html-extract):

"{'2018-05-02', '2018-01-05', r, '2018-07-01', '2017-07-02', '2016-07-31' random_text XYCCC Letters and 55565798 ]}"

My intention is, to extract all values from '2018-05-02' ... to (and excluding) random_text. I tried to achieve this through chosing the "anything but" structure to achieve this [^a] (not a):

\'[^random]*

The above does not do the job, because random is not a string, but a set of characters, hence the 'r' in the string will split my extracted value.

If there is no r in the text before the word random_text, this would work fine:

\'[^r]*

Is there any way to include a specific string as the end of my sequence. e.g.

start: \' repeated characters unlike string: [^{my_string}]*

Appreciate any insight :)

标签: regex

解决方案


This regex will do the job:

'.+'(?= random)

Just replace random with the string you want to exclude at the end.

Demo & explanation


推荐阅读