need help to create a regular expression for this

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
mgwalk
Forum Newbie
Posts: 3
Joined: Sat Jul 17, 2010 4:43 pm
Location: Ohio

need help to create a regular expression for this

Post by mgwalk »

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
Last edited by mgwalk on Sat Jul 17, 2010 5:04 pm, edited 1 time in total.
User avatar
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

Post by AbraCadaver »

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.
mgwalk
Forum Newbie
Posts: 3
Joined: Sat Jul 17, 2010 4:43 pm
Location: Ohio

Re: need help to create a regular expression for this

Post by mgwalk »

Yes there is always a space before the \r\n
User avatar
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

Post by AbraCadaver »

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.
mgwalk
Forum Newbie
Posts: 3
Joined: Sat Jul 17, 2010 4:43 pm
Location: Ohio

Re: need help to create a regular expression for this

Post by mgwalk »

Thanks for the example. How about just regular expression no php?
User avatar
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

Post by AbraCadaver »

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.
User avatar
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

Post by ridgerunner »

remove the decimals:

Code: Select all

$str = preg_replace('/\b(\d+)\.\d+\b/', '$1', $str);
replace the spaces with a comma and remove the space + carriage return + line feed

Code: Select all

$str = preg_replace('/[ \r\n]+/', ',', $str);
Hope this helps :)
Post Reply