Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Pattern Matching

Pattern Matching

Pattern Matching

I want to replace all lines in a file with just ,(comma) to be null,

,        <linefeed>
  ,   <linefeed>

I tried

$aString=~ s/^\s*?,\s*?$/null,/g;
$aString=~ s/^\s?,\s?$/null,/g;

but no luck!

RE: Pattern Matching

you don't need the '?' in it, but that doesn't matter.  i tested both your first regex and the one i have here many times with a dummy file, and it worked perfectly.  here's mine:
while (<FILE>) {

it may be that the problem isn't the regex, but the way you're calling it...
sorry i couldn't help any more.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Pattern Matching

This replaces the comma lines with the value of null, not the string.  If you want the string, just put 'null' in the right side.  The way you were approaching the pattern match, the '^' and '$' pinned the match to the beginning and end of the file...... not the beginning and end of a line.  So, just treat the '\n' as any other character and match it.  Like this,

$lines = `cat junk.txt`;
$lines =~ s/\s*?,\s*?\n//g;
print "lines - $lines";

Where junk.txt looks like,
some, text,


 keep the rudder amid ship and beware the odd typo

RE: Pattern Matching

I found out that I need to use the /gm switch.
Don't quite know exactly what the /m is, but I suspect there is a Windows/UNIX linefeed problem.  I used UltraEdit, and even saving test file as UNIX format did not work without the /m switch?

I guess you guys must have tried my script out in UNIX/Linux?

RE: Pattern Matching

/m specifies that the match be done with multi-line behavior.  were you reading the file in one line at a time, and checking that against the regex, or did you read in the file as one scalar variable, and then test that?

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

RE: Pattern Matching

the $scalarVar contains the whole file, does it mean I need to us /m??  Not quite sure about the meaning of mult-line!

RE: Pattern Matching

yes, then use /m.  it has to do with how it defines certain things, like "." and "$", specifically whether or not they'll match on a newline.  basically, if there's more than one line of text in the pattern to be matched, specifiy /m and and use the regex like usual, and i should work like usual.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close