1

Is it possible to detect something like this using regex? If so how would I do it. I am using the regex search in Sublime Text and I want to find code matching the below.

=> array( ... contents ... ),

and here is an example of one of the code blocks I want to find

=> array(
    'title' => __('Subtle Pattern', 'nhp-opts'),
    'img' => get_template_directory_uri() . '/skins/images/subtlepattern-swatch.jpg '
),
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
byronyasgur
  • 4,627
  • 13
  • 52
  • 96
  • Please be more precise: provide a typical input, and the expected output. – sp00m Mar 29 '13 at 15:31
  • 1
    sorry I thought it was obvious I wanted to know how. I have never used regex before. I will update my question – byronyasgur Mar 29 '13 at 15:32
  • @ExplosionPills You wanted him to add *"If yes, how?"*? Quite implicit... – sp00m Mar 29 '13 at 15:34
  • It's not straightforward to find matching (closing) parenthesis for array function though. – anubhava Mar 29 '13 at 15:34
  • What language are you working in? How does your array look like? What result do you want to get (filtered array, first matched index)? What pattern are you looking for to match - provide a list of sample strings the regex should/should not match? – Bergi Mar 29 '13 at 15:35
  • @byronyasgur it is obvious, but you need to expand on your question. For starters you didn't say what flavor (it was just tagged as "regex," but this has been corrected). You also don't give any specifics about *what* you want to detect, and under what circumstances. Your example is easily matched by the regexen `=`, `=>`, `=> a`, and `=> array\(.*?\)`, but which is appropriate for you? – Explosion Pills Mar 29 '13 at 15:36
  • @Bergi PHP. I'm using the regex search in sublime text. It just finds part of the code based on some regex. I dont think there's any output as such. – byronyasgur Mar 29 '13 at 15:36
  • @byronyasgur: Ah, thanks. So do you want to match all arrays, or only those that contain the (exact) string `contents`? – Bergi Mar 29 '13 at 15:37
  • Sorry like I said i've never really used regex. Didnt realise my question was so vague. – byronyasgur Mar 29 '13 at 15:38
  • @Bergi sorry not sure why what i'm asking is unlclear though I'm sure it is. What I have is a file with several versions of teh above code ( different contents in each ) and I want to select them using the regex search in sublime – byronyasgur Mar 29 '13 at 15:39
  • @byronyasgur Sublime Text doesn't support recursive regex. You'll have a hard time doing so. Maybe we can figure something out knowing what you want to accomplish: why do you want to match those contents? – Loamhoof Mar 29 '13 at 16:15
  • @Loamhoof to cut a long story short I'm basically just editing a large file with lots of those and wanted to delete everything that pattern ... but at this stage I could do it manually easier but I'd still like to learn ... I wouldn't have asked If I had realised it was so difficult. apologies SO – byronyasgur Mar 29 '13 at 16:19
  • @byronyasgur matching recursive patterns is really hard but can be done. Something along the lines of [that](http://stackoverflow.com/questions/3644266/) (a harder version though ;)). Some languages allow recursive regex so it would have been easier with a php script for example. But you're right, better learning ;) – Loamhoof Mar 29 '13 at 16:22
  • @Loamhoof yes so much for getting my feet wet ... maybe regex needs to be swallowed in fairly large chunks not nibbled at. Will do a web tutorial on it before any more mucking around – byronyasgur Mar 29 '13 at 16:25
  • @byronyasgur You'll find everything you need [here](http://www.regular-expressions.info/tutorial.html). Have a good day :) – Loamhoof Mar 29 '13 at 16:26

1 Answers1

0

Tested in Sublime text as well, finds and deletes those blocks just fine.

=> array\(\s*\n(?:\s*.+\n)*\),

Optionally, if all of your array items are indented properly, you could:

=> array\(\s*\n(?:\s{4}.+\n)*\),

Note: this is for arrays of the format that you specified:

=> array(
    ...foo...
    ...bar...
    ...buzz...
),
methai
  • 8,835
  • 1
  • 22
  • 21