Need regex to extract terms separated by space and key/value pairs separated by colon?

时间:2016-02-12 21:27:38

标签: php regex

I need a regex to process the keywords from a search field. The idea is to have it act very much like google (ie: support double quoted text to allow for terms with spaces). Further, I have tags in my dataset that I would like to be able to reference using a color.

A sample input would be:

select <stuff> from <table>
where replace(replace(upper(id), 'I', '1'), 'O', '0') like '%<number-search-term>%'

This should output:

1/2 one two "three & four" five:six seven:"eight nine ten"

It would also work fine to have 'five' and 'seven' in the same capture group as the other keywords as long as there is another capture group associated with 'six' and 'eight nine ten'.

1 个答案:

答案 0 :(得分:0)

Regex:

Extension server_name, server_name: [type=host_name (0), value=site.company.com]

Substitution:

((\"[\s\S]+?\")|[\s\S]+?(:(?2))?)\K(\s+|$)

Explanation:

Get everything and possibly everything in-between quotes (starting with either spaces or a \r\n ), then resets the regex matching, and then matches any spaces or end of the string. This means that it only matches the spaces or end of string and then replaces it with a newline.

Use:

:

Demo: https://regex101.com/r/jO4oM0/3

Edit 1: Improved as wasn't properly matching everything properly
Edit 2: Shortened