Replacing the last space

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

Moderator: General Moderators

Post Reply
theozaf
Forum Newbie
Posts: 2
Joined: Wed Sep 19, 2007 3:08 am
Location: Greece/Athens

Replacing the last space

Post by theozaf »

Hello guys I am really new to your forum, I have searched through the pages to find a solution before posting but didn't find anything matching my question.....

Well I have a string which always ands by ; I mean string ; and in that point I want to replace the space between the string and ; character. what is the correct expression to say replaceAll("\\s+\\;", "") I have tried this and it doesn't work....

thank you
theozaf
Forum Newbie
Posts: 2
Joined: Wed Sep 19, 2007 3:08 am
Location: Greece/Athens

Post by theozaf »

OK I found the solution my own just for anyone who needs in the future

"\\s+\\;", ";" otherwise it replaces the space and the ; together
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Code: Select all

preg_replace('/\s++(?=;)/', '', $str);
regexpert
Forum Newbie
Posts: 7
Joined: Sat Oct 20, 2007 3:41 am

Another solution

Post by regexpert »

Code: Select all

preg_replace("/(.*?)\s+(;)\s+/","$1$2",$string);
Post Reply