Page 1 of 1

Simple regex problem (I think so anyway)

Posted: Tue Dec 29, 2009 11:35 am
by AlanG
I'm so hopeless with regex. Was hoping someone could give me a helping hand. :)

I've got a file with various strings on each line. I want to add a new line character after each of the following patterns. (nxt is a literal string and N is an integer)

Code: Select all

 
Any number of varying characters-NNNNN
Any number of varying characters-NNNNN-NN
nxt
 
Any ideas? :) And thanks for any suggestions.

Re: Simple regex problem (I think so anyway)

Posted: Tue Dec 29, 2009 12:51 pm
by AbraCadaver
Just quickly tested:

Code: Select all

$file = preg_replace('/((.+-[\d]{5}(-[\d]{2})?)|(nxt))/', "$1\n", $file);

Re: Simple regex problem (I think so anyway)

Posted: Tue Dec 29, 2009 1:39 pm
by AlanG
AbraCadaver wrote:Just quickly tested:

Code: Select all

$file = preg_replace('/((.+-[\d]{5}(-[\d]{2})?)|(nxt))/', "$1\n", $file);
Excellent! Thank you very much. Tested it and it works perfectly. :)

Re: Simple regex problem (I think so anyway)

Posted: Tue Dec 29, 2009 11:04 pm
by ridgerunner
AbraCadaver wrote:Just quickly tested:

Code: Select all

$file = preg_replace('/((.+-[\d]{5}(-[\d]{2})?)|(nxt))/', "$1\n", $file);
This can be simplified a bit. You don't need to enclose the \d within a character class. Also, the two alternatives don't need to be in parentheses...

Code: Select all

$file = preg_replace('/(.+-\d{5}(-\d{2})?|nxt)/', "$1\n", $file);