Does anyone here know how to do some relatively simple regex (regular expressions) strings?
Example I have a list like this:
~one,
~two,
~three,
~four,
~five,
Then using a simple regex to remove the backspaces with find and replace:
Find: ~
Replace: \b
To make the list look like this:
one,two,three,four,five,
Found and translated into PHP code with RegexBuddy 3 app.
First search for and remove all “~” with “”.
Then you’ll have
one,
two,
three,
four,
five,
Then replace “\r\n” with “”.
Then you’ll have
one,two,three,four,five,
$result = preg_replace(‘/\r\n/’, ‘’, preg_replace(‘/~/’, ‘’, $subject));
Hey Matt, Thanks for the help and code, I’m using EditPadPro, I got your example to work but I don’t know how to tell,with regex, to ignore blank lines? example:
one,
two,
three,
four,
five,
should create:
one,two, three
four,five
so four,five would stay on their own line as where as your solution works but merges all onto one line like one,two,three,four,five ect…if that makes sense.
Actually, I just tested it with the one above and it worked just the same. The one above removes all returns and even double ones.
This regular expression tells it to find all (1 or more) instances of \r, \n, and ~ and replace them with an zero length string. The /s tells it to treat everything as if it were one line.
$result = preg_replace(/[\r\n~]+/s, ‘’, $subject);
thanks for your help guys, the explanations make sense, however I still can’t get the regex code to ignore blank lines, Matt, I looked at your screenshot and it still looks like it’s throwing everything onto 1 line, maybe I’m not makign sense…lol
I want:
line one,
line two,
line three,
(regex ignore this space)
line four,
line five,
line six,
to look like:
line one,line two,line three,
(regex ignore this space)
line four,line five,line six,
thanks for your help guys, the explanations make sense, however I still can’t get the regex code to ignore blank lines, Matt, I looked at your screenshot and it still looks like it’s throwing everything onto 1 line, maybe I’m not making sense…lol
I want:
line one,
line two,
line three,
(regex ignore this space)
line four,
line five,
line six,
to look like:
line one,line two,line three,
(regex ignore this space)
line four,line five,line six,
I kinda have it working but it’s a little ghetto…
You’ve offered a couple of different formats for source list, so working on the assumption you want to turn:
~one,
~two,
~three,
~four,
~five,
– blank line –
~six,
~seven
To:
one, two, three, four, five,
– blank line –
six, seven
$find = array(
"/~/s",
"/(,[\r\n]+)/sU",
);
$replace = array(
'',
','
);
$str = preg_replace($find, $replace, $str);
The code above doesn’t leave spaces between the commas… the following change should give you the spacing:
$find = array(
"/~/s",
"/(,[\r\n]+)/sU",
);
$replace = array(
'',
', '
);
$str = preg_replace($find, $replace, $str);
Thanks guys, i’m sorry for not being clear, I’m not using this in a php file, I’m converting the data from large .txt files in a basic regex capable text editor, I figured out a way that works but havn’t been able to stop it from removing blank lines, I’ve been using:
Search string: ,\r\n
Replace String: ,
and so far this turns:
line one,
line two,
line three,
– blank line –
line four,
line five,
line six,
into:
line one,line two,line three,
line four,line five,line six,
but I want it to look like:
line one,line two,line three,
– blank line –
line four,line five,line six,
If you still have at least one line you can replace on a second round with
Search String: \r\n
Replace String \r\n\r\n
If (P) represents one \r\n in
line one,line two,line three,(P)
line four,line five,line six,
Then it should add another line in between.