Search Replace

I have an array that I want to remove every instance of '$' and all
leading and trailing white space.
My code only seems to remove '$'. I know I can split this into separate
commands but is there a way to do this all at once?

$_=~s/($\s+)||(\s+^)||(\$)//g foreach (@fields);
I have also tried
$_=~s/[($\s+)(\s+^)(\$)]//g foreach (@fields);


0
pkraus
2/3/2003 3:29:14 PM
perl.beginners 29388 articles. 4 followers. Follow

4 Replies
615 Views

Similar Articles

[PageSpeed] 37
Get it on Google Play
Get it on Apple App Store

 .------[ Paul Kraus wrote (2003/02/03 at 10:29:14) ]------
 | 
 |  I have an array that I want to remove every instance of '$' and all
 |  leading and trailing white space.
 |  My code only seems to remove '$'. I know I can split this into separate
 |  commands but is there a way to do this all at once?
 |  
 |  $_=~s/($\s+)||(\s+^)||(\$)//g foreach (@fields);
 |  I have also tried
 |  $_=~s/[($\s+)(\s+^)(\$)]//g foreach (@fields);
 |  
 `-------------------------------------------------

    Try this regex instead: 

    s/(?:^\s+|\$+|\s+$)//g

 ---------------------------------
   Frank Wiles <frank@wiles.org>
   http://frank.wiles.org
 ---------------------------------

0
frank
2/3/2003 3:43:56 PM
"Paul Kraus" <pkraus@pelsupply.com> wrote in message
news:005b01c2cb99$02315ee0$8afea8c0@pkraus...
> I have an array that I want to remove every instance of '$' and all
> leading and trailing white space.
> My code only seems to remove '$'. I know I can split this into
separate
> commands but is there a way to do this all at once?
>
> $_=~s/($\s+)||(\s+^)||(\$)//g foreach (@fields);
> I have also tried
> $_=~s/[($\s+)(\s+^)(\$)]//g foreach (@fields);

Hi Paul.

I assume you mean whitespace preceding and following the dollar?

    s/\s*\$\s*//g foreach @fields;

I'm sure you knew the answer really!

Cheers,

Rob



0
rob
2/3/2003 3:54:21 PM
Nope I wish.

3 fields. 1 is a part number, 2 is cost, 3 is list.
I am removing the '$' for 2 and 3. where as I am removing white space
from 1,2,&3.
I was just trying to do it all in one statement.

Frank Wiles solution worked perfectly.
s/(?:^\s+|\$+|\s+$)//g

Paul
-----Original Message-----
From: Rob Dixon [mailto:rob@dixon.nildram.co.uk] 
Sent: Monday, February 03, 2003 10:54 AM
To: beginners@perl.org
Subject: Re: Search Replace



"Paul Kraus" <pkraus@pelsupply.com> wrote in message
news:005b01c2cb99$02315ee0$8afea8c0@pkraus...
> I have an array that I want to remove every instance of '$' and all 
> leading and trailing white space. My code only seems to remove '$'. I 
> know I can split this into
separate
> commands but is there a way to do this all at once?
>
> $_=~s/($\s+)||(\s+^)||(\$)//g foreach (@fields);
> I have also tried
> $_=~s/[($\s+)(\s+^)(\$)]//g foreach (@fields);

Hi Paul.

I assume you mean whitespace preceding and following the dollar?

    s/\s*\$\s*//g foreach @fields;

I'm sure you knew the answer really!

Cheers,

Rob




-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

0
pkraus
2/3/2003 4:20:28 PM
On Mon, 03 Feb 2003 10:29:14 -0500, Paul Kraus wrote:

> I have an array that I want to remove every instance of '$' and all
> leading and trailing white space.
> My code only seems to remove '$'. I know I can split this into separate
> commands but is there a way to do this all at once?

Erm, I don't want to be offensive, but to do it in 3 commands is still the
best. These are 3 different jobs and your code will be much more
maintainable if they aren't munged together! I believe also that it will
the quickest _not_ to mix them together.
Just write the quick and short solution:

tr/$//d, s/^\s+//, s/\s+$// for @fields;

Note that the , still enables to write it as one liner.

> 
> $_=~s/($\s+)||(\s+^)||(\$)//g foreach (@fields);
> I have also tried
> $_=~s/[($\s+)(\s+^)(\$)]//g foreach (@fields);


Cheerio,
Janek
0
bigj
2/4/2003 2:17:05 PM
Reply:

Similar Artilces:

Dumb question from beginner
Hello - Just learning some basic things with ASP.NET (using VB). I have 2 variables, 1 has a sentance in it, ie : variable1 = "The rain in %country% falls mainly on the plain." variable2 = "Spain" I want to replace %country with Spain. What is the syntax for this? Thanks, Dan Dim variable1 As String Dim variable2 As String Dim variable3 As String variable1 = "The rain in %country% falls mainly on the plain." variable2 = "Spain" variable3 = variable1.Replace( "%...

Search not searching
Hey All, I have a simple asp.net & vb.net form that retrieves product data based on the user entering a product#.  My users are able to search for multiple product #s and have the corresponding data returned.  This all works fine when there is something to return to my datagrid.  When there isn't, you should be able to enter another product# and search again, but it doesn't work.  If there's nothing to return in my datagrid, the Search button won't search again. What's happening is simple to see on the page but hard to explain.  My code is b...

search an replace
Hi This scripts sucks in a 109mb file and i'm trying to do a search and replace on unxtime to the format from strftime. Which is working... But I run this system call and it took allnight to run :( So I killed it... Any other suggestions to reach my goal..... #!/usr/bin/perl use strict; use POSIX 'strftime'; my $f = "/tmp/ip.txt"; open (FILE, "< /tmp/ip.txt") || die ("Open Failed: $!\n"); my @lines = <FILE>; # file into array foreach (@lines) { next if /^S/; next if /^-/; my @line = split(/\t/, $_); ...

search replace
Hi , How do I search replace text in a file from a perl script. i.e. by opening the file in write mode and then do a search replace. I don't want to do a command line search replace. Regards Rajeev > Hi , > > How do I search replace text in a file from a perl script. i.e. by > opening the file in write mode and then do a search replace. > > I don't want to do a command line search replace. Use the s/// operator...as follows: $_ =~ s/oldtext/newtext/gi; this will replace all occurrences of "oldtext" with "newtext". You s...

search and replace
Hi, In one of my scripts I do a search and replace like this : $text =~ s/$search/$replace/; This works fine, but if $search or $replace contains one or more words between round brackets () it doesn't what I had in mind : $text = "This is a text (with brackets)..."; $rearch = "(with brackets)"; $replace = "without brackets"; $text =~ s/$search/$replace/; print $text; If I run this example it prints : This is a text (without brackets)... Where did I go wrong ? Kind regards, Rene Verharen Plea...

search and replace
------=_NextPart_000_0007_01C11CEF.5A6AE180 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable i need to search for &member& in a textarea and replace it with the = contents of $member. how can i do this? im really quite new to this. :) im pretty sure that this will search it out $textarea =3D~ s/&member&/I; will search it out, but not sure on how to replace it when the script = sends the email. ------=_NextPart_000_0007_01C11CEF.5A6AE180-- On 8/4/01 12:11 PM, Steven MysticPoet wrote: > i need to sea...

Search and replace
Hello, I want to make a command line application that search for *.htm* files inside directories and subdirectories. 1. How can I save all the *.htm* files inside directories and subdirectories inside a array? Can you give me an example? 2. I want to search inside these files. And if found... replace some text and save the file under the same name. Can you give me an example? Thanks! ShadowDanserPlease remember to 'Mark as Answer' if this post answered your question! You can get all the .htm files like this: string[] files = System.IO.Directory.Get...

Replace "Google" caption in search box with "search"
Name: Product: Firefox Summary: Replace "Google" caption in search box with "search" Comments: You are unnecessarily promoting google brand. Apple did away with "google" caption on iphone search with just "search". May not be unnecessary as google sponsors for making that search engine, but there is a favicon showing the search engine being used is google, also tooltip on mouseover search box shows "search with google", so caption that always shows is redundant. Browser Details: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US;...

tools to do multi-pattern/regex based multi-file searching+search/replace?
I'm looking for a generic tool that could be used to define a bunch of patterns (eg., regex expressions) to identify common coding idioms in projects and catalog them, as well as allow them to be replaced by another string. I'm aware of one called ReplaceEM or ReplaceText, but I'm wondering if there's anything that may be more specifically tailored to programming tasks. > {quote:title=David Schwartz wrote:}{quote} > I'm looking for a generic tool that could be used to define a bunch of patterns (eg., regex expressions) to identify common coding idioms in project...

How do I perform search and or replace on multiline textbox text to replace a Template Tag with members data
I have a project that's almost done and have to do a webinar on it tomorrow so I'm desperate to find a solution. I have created custom email templates to be used outside my application to be copied and pasted into this outside 3rd party application that we are interfacing with but not programatically unfortuantely. I'm providing our members with the ability to read in the email template html stored in a txt file. Inside these txt files I've replaced the href link with a tag so that each members personal domain name or system url will be inserted in place of this template...

drag and drop to Google search box inserts rather than replaces text from prior search
Name: Don Product: Shiretoko Summary: drag and drop to Google search box inserts rather than replaces text from prior search Comments: drag and drop of selected text to Google search box inserts rather than replaces text from prior search. If the selected text is a hyperlink, the URL is pasted instead of the text. This is different from past versions and frankly, not useful (bug v. feature) Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1b3) Gecko/20090305 Firefox/3.1b3 From URL: http://hendrix.mozilla.org/ Note to readers: Hendrix gives no expec...

search and replace help...
How would I remove all the space and replace them with commas, but where there is more than one space in a row, only replace those with one comma? In other words make the follwing file comma delimited and strip out everything preceding the date? === My crack at the script === use strict; while(<>) { s/\s/,/g; s/^.*?(\w{3} \w{3}\s+\d+.*)$/$1/; # modified slightly print; } ===== end ==== === Input Log file ==== 172.16.54.132 ssjobhnd Tue Sep 03 07:00:03 2002 SNBJH_3075J Syncsort Backup Express version 2.1.5; Copyright Syncsort Incorporated, 1996-2002 17...

Urgent :search and replace
------=_Part_4652_8113828.1150960010693 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi Experts, I have wired problem , I have search and replace string in very huge text file . The problem with the string is broken in the differenet line. I'm having a string 'ab=test1, db=test2,dc=test3' and I want to replace this with 'ab=chk1,bc=chk2'. This string in the file is present in various fasion like <File> ..........................ab=tes t1, db=test2,dc=test3'.... ............

search and replace #9
--1105474026-2036035012-1345251743=:19147 Content-Type: text/plain; charset=us-ascii hi, i have string like this : $a = '$(workspace)\convergence\trunk'; i need to replace $(workspace) with 'c:\p4\abc' i wrote regex like this : $a =~ s/$\(workspace)/c:\\p4\\abc/; however, the string which i am getting is : $(c:\p4\abc)\convergence\trunk i need output like this : c:\p4\abc\convergence\trunk somehow, i am not getting, how to escape, $ and braces please suggest regards irfan --1105474026-2036035012-1345251743=:19147-- On Fri, 17 Aug 2012 18:02:2...

Web resources about - Search Replace - perl.beginners

File:Replace this image male.svg - Wikipedia, the free encyclopedia
to an image (e.g. you photographed or drew it yourself) and would like Wikipedia to use it, you must release it under a free copyright license ...

Facebook Replaces Payments Emails With API
Facebook announced that it is scrapping the emails that it sends to developers containing information about payments , replacing it with a payments ...

Facebook’s New Ads Power Editor Replaces the Bulk Uploader with a Streamlined GUI
This week, Facebook launched its new Ads Power Editor desktop software for buyers who work directly with a Facebook ads representatives. The ...

Face Morph HD - Change and Replace Head in Pic Frame Hole with Pencil Portrait Effects! on the App Store ...
Get Face Morph HD - Change and Replace Head in Pic Frame Hole with Pencil Portrait Effects! on the App Store. See screenshots and ratings, and ...

NASA just chose SpaceX to replace the Shuttle for flying astronauts
jurvetson posted a photo: Elon's design goal = "the safest, most advanced crew vehicle ever flown.” With seats for seven . First manned flight ...

How to replace Roland Juno 106 Voice Chip 80017A 1 of 2 - YouTube
Own a Juno 106? You might encounter a dying voice chip. In this 2 part vid series I explain how to fix this problem. Don't be afraid its really ...

Khurram Khan ‘is going to be very difficult to replace’ for UAE ahead of World Twenty20 qualifiers
The post-Khurram era starts when the UAE face the hosts Scotland in the opening fixture of the World Twenty20 qualifier. The national team have ...

Change of the Day: Canadian Town Wants to Replace Its ‘Land of Rape & Honey’ Motto
The town of Tisdale in Saskatchewan has had a very unfortunate slogan for the past 60 years, and they’re finally looking for a new one. Visitors ...

ASI helps Cloud wholesaler replace Cisco hardware with Brocade rentals
ARNnet ASI helps Cloud wholesaler replace Cisco hardware with Brocade rentals ARNnet Cloud Plus is transitioning from Cisco to Brocade hardware ...

Notable names floated to replace Georgie Gardner on Today
Two names have raised eyebrows within television circles as Channel Nine prepares to announce a replacement newsreader on its breakfast television ...

Resources last updated: 12/18/2015 9:27:22 PM