there is for example a string

param0_test1_param1_affiliatewire_%nameID%_test2_param2_param3 

how can I find an affiliatewire_%nameID% where %nameID% always a different word and output %nameID%
affiliatewire_ <- always occurs! string examples:

 param0_test1_param1_affiliatewire_name1_test2_param2_param3 param0_test1_param1_affiliatewire_name_test2_param2_param3 param0_test1_param1_affiliatewire_trollolo_test2_param2_param3 

Thank!!

2 answers 2

Decision:

 <?php $re = "/(?<=affiliatewire_)[^_\r\n]++/"; $str = "param0_test1_param1_affiliatewire_name1_test2_param2_param3 param0_test1_param1_affiliatewire_name_test2_param2_param3 param0_test1_param1_affiliatewire_trollolo_test2_param2_param3"; preg_match_all($re, $str, $matches); print_r($matches); 

Result:

 Array ( [0] => Array ( [0] => name1 [1] => name [2] => trollolo ) ) 

Regular Explanation Explanation:

(?<=affiliatewire_) - we are looking for text preceded by the expression affiliatewire_ .
[^_\r\n]++ - we are looking for the longest possible sequence of characters except _ and line-break characters.


You can test the PHP code on Ideone , and the regular expression on regex101 .

  • and if like this: `/ affiliatewire _ (. [^ _] *) _ /` ?? - John Freeman
  • @JohnFreeman Does not capture the value if it is the last in a line. For example, like this: param0_test1_param1_affiliatewire_name will not find. I gave you a link to the service for verification. - VenZell
  • thank you so much - John Freeman
 /(?<=[_\n]|^)affiliatewire_([^_\n]*)/ 

The meaning of a regular expression:

  • text affiliatewire_ at the beginning of the line, the beginning of the text or after the literal _
  • in the first group we capture all subsequent text, except _ and line break

https://regex101.com/r/tC2iX5/1