简体   繁体   中英

regex to match a multi-line string

Sorry, for the badly named topic. I'm looking for a efficient regular expression to match the following:

[2014-11-13 13:05:04] [path/to/class.instagram.php] Options: array (
  'client_id' => '',
  'client_secret' => '',
  'object' => 'tag',
  'object_id' => 'rwar',
  'aspect' => 'media',
  'callback_url' => 'http://www.localthisday.nl/instagram.callback.php',
)

I want it to be split up so it looks like this:

1. [2014-11-13 13:05:04]
2.[path/to/class.instagram.php]
3. Options: array (
  'client_id' => '',
  'client_secret' => '',
  'object' => 'tag',
  'object_id' => 'rwar',
  'aspect' => 'media',
  'callback_url' => 'http://www.localthisday.nl/instagram.callback.php',
)

I'm using this one at the moment:

(\\[(.*)\\])(\\s{1})(\\[(.*)\\])(\\s{1})(.*)/gmi

I know, it's not a very good one, that's probably why it doesn't work the way I want it to work. Problem is; it's a multi-line string, and for some reason it does not work multi-line. Could someone please help me? I'm not that good in regular expressions unfortunately.

Just replace the last (.*) with ([\\S\\s]*) in your regex.

/(\[.*\])\s(\[.*\])\s([\S\s]*)/gmi

DEMO

See the captures at the right side in the above demo link.

使用非贪婪量词:

(\[.*?\])\s(\[.*?\])\s(.*)/gmi
(\]|\))

Try this.Replace by $1\\n .See demo.

http://regex101.com/r/pQ9bV3/32

var re = /(\]|\))/gm;
var str = '[2014-11-13 13:05:04] [path/to/class.instagram.php] Options: array (\n \'client_id\' => \'\',\n \'client_secret\' => \'\',\n \'object\' => \'tag\',\n \'object_id\' => \'rwar\',\n \'aspect\' => \'media\',\n \'callback_url\' => \'http://www.localthisday.nl/instagram.callback.php\',\n)';
var subst = '$1\n';

var result = str.replace(re, subst);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM