-3

I'm trying to write RegEX to match all eventtypes in next string (should end with whitespace character or bracket.

(STUNNEL-SECURITY AND NOT (
    eventtype=sec-stunnel-client-* 
 OR eventtype=sec-stunnel-crl-* 
 OR eventtype=sec-stunnel-loaded-* 
 OR eventtype=sec-stunnel-loading-* 
 OR eventtype=sec-stunnel-no-* 
 OR eventtype=sec-stunnel-openssl-* 
 OR eventtype=sec-stunnel-sconnect-* 
 OR eventtype=sec-stunnel-server-* 
 OR eventtype=sec-stunnel-service-* 
 OR eventtype=sec-stunnel-session-* 
 OR eventtype=sec-stunnel-socket-* 
 OR eventtype=sec-stunnel-ssl-* 
 OR eventtype=sec-stunnel-stats-* 
 OR eventtype=sec-stunnel-threading-* 
 OR eventtype=sec-stunnel-timeout-* 
 OR eventtype=sec-stunnel-various-* 
) )

I need to get array of all matches, eg. array("sec-stunnel-client-*", "sec-stunnel-crl-*", "sec-stunnel-loaded-*"...).

How this should be done?

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
  • If you have tried something, please add your attempt, including where you're stuck and expected output, to your question. If you haven't tried anything, you should do that before asking. SO isn't a free coding service. – M. Eriksson Mar 16 '18 at 11:48
  • write a regex that searches for 'eventtype=' with a lookahead until the next ' '. Related: https://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups – Jeff Mar 16 '18 at 11:48

2 Answers2

2

Try this regex:

(?<=eventtype=)\S*

It looks for non-whitespace characters preceded by eventtype=. Try it online here.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
  • 1
    This might do the trick, but it's generally frowned upon answering questions where the OP doesn't show any effort at all of trying to solve the issue themselves. – M. Eriksson Mar 16 '18 at 11:59
1

You can use \K instead of positive lookahead (?<=) far less steps.

Regex: eventtype=\K\S+

Details:

  • \K Resets the starting point of the reported match
  • \S+ Matches any non-whitespace character between one and unlimited times

PHP code:

preg_match_all("~eventtype=\K\S+~", $str, $matches);

Output:

Array
(
    [0] => sec-stunnel-client-*
    [1] => sec-stunnel-crl-*
    [2] => sec-stunnel-loaded-*
    [3] => sec-stunnel-loading-*
    [4] => sec-stunnel-no-*
    ...
)

Code demo

Srdjan M.
  • 3,310
  • 3
  • 13
  • 34