here is my string multiple lines
368.7,633.2 368.7,633.2 369.2,633.8 369.4,634.2 369.9,634.2 370,634.2 \r\n
370.6,634.3 371,634 371.3,633.7 371.6,633.9 372.2,634.5
I need to create a regular expression to this string basically
remove the decimals
replace the spaces with a comma
and remove the space + carriage return + line feed
368,633,368,633,369,633,369,634,369,634,370,634,370,634,371,634,371,633,371,633,372,634
need help to create a regular expression for this
Moderator: General Moderators
need help to create a regular expression for this
Last edited by mgwalk on Sat Jul 17, 2010 5:04 pm, edited 1 time in total.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: need help to create a regular expression for this
Is there always going to be a space before the \r\n?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: need help to create a regular expression for this
Yes there is always a space before the \r\n
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: need help to create a regular expression for this
Dunno why I asked that, doesn't matter.
Just a little regex:
Code: Select all
$new_string = implode(',', array_map('intval', explode(',', preg_replace('/[\s]+/', ',', $string))));mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: need help to create a regular expression for this
Thanks for the example. How about just regular expression no php?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: need help to create a regular expression for this
Ahhh sorry, I didn't pay attention to the forum this was in. Maybe a regex guru will answer, but for me, I can't see it without two regex replaces. This is PHP, but the patterns should be valid for whatever language you're using:
Code: Select all
$new_string = preg_replace('/\.[\d]+/', '', preg_replace('/[\s]+/', ',', $string));mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
- ridgerunner
- Forum Contributor
- Posts: 214
- Joined: Sun Jul 05, 2009 10:39 pm
- Location: SLC, UT
Re: need help to create a regular expression for this
remove the decimals:
replace the spaces with a comma and remove the space + carriage return + line feed
Hope this helps 
Code: Select all
$str = preg_replace('/\b(\d+)\.\d+\b/', '$1', $str);Code: Select all
$str = preg_replace('/[ \r\n]+/', ',', $str);