LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2006, 12:02 PM   #1
tooparam
Member
 
Registered: Nov 2005
Location: India
Distribution: Slackware
Posts: 51

Rep: Reputation: 15
Simple Regular Expression in PHP


Dear

I want to remove every occurence of /* */ from a string. For example

aaaaaaaaa/*bbbbbbbbb*/ccccccc/*dddd*/eeeeeeeeee

should become

aaaaaaaaaccccccceeeeeeeeee

Can you provide me the necessary Regular Expression for that.


Thanks,
Paramvir
 
Old 03-09-2006, 12:17 PM   #2
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Rep: Reputation: 30
I don't know about PHP, but in perl you would just do this:
[code]#If in file:
while(<>) {
if (m:/\*.*\*/ {
$fixed = $_;
$fixed =~ s:/\*::
print "$fixed\n";
}
}
 
Old 03-09-2006, 12:43 PM   #3
schentor
Member
 
Registered: Jul 2005
Distribution: RHEL, Mint, Ubuntu
Posts: 32

Rep: Reputation: 15
This should work:
Code:
$output = preg_replace("/\/\*.*?\*\//", "", $str);
where $str is the original string and $output is the new string.
 
Old 03-09-2006, 12:44 PM   #4
tooparam
Member
 
Registered: Nov 2005
Location: India
Distribution: Slackware
Posts: 51

Original Poster
Rep: Reputation: 15
Thanks for the reply.

I will try it and let you know the results.

Last edited by tooparam; 03-09-2006 at 12:45 PM.
 
Old 03-09-2006, 01:08 PM   #5
airswit
Member
 
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89

Rep: Reputation: 15
i tried the following one, and it gives me a PHP error, so i'm not sure what is going on. the regex is /\*.*?\*/ , according to : http://www.regular-expressions.info/...rogrammer.html . the question mark is needed to make sure that it does not extend to the last *\, but you can read about it on the site i showed, under the 'comments' section

Last edited by airswit; 03-09-2006 at 01:15 PM.
 
Old 03-09-2006, 02:21 PM   #6
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Rep: Reputation: 30
Whoops looks like on my post it auto converted some of my code to a smily face - sorry - that should have been a colon )
 
Old 03-09-2006, 11:06 PM   #7
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
There is a great reg exp builder in KDE. Very useful to build visually.

KRegExpEditor.

It's quite cool to build regular expressions without having to worry about the syntax but just thinking logically about what you want to match.
 
Old 03-09-2006, 11:22 PM   #8
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
in perl somthing like this..
Code:
$line =~ s/\/\*.+?\*\///g;
edit> oops didnt see schentor's post.. it should work..

Last edited by xhi; 03-09-2006 at 11:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
php regular expression for mm-dd-yy opioid Programming 7 03-17-2005 03:22 PM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM
using a perl regular expression in php markus1982 Programming 5 11-18-2002 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration