Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substituting a varibale in a regex 1

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

In relation to my post "Count commas from input file for output file" I think I may be able to solve the puzzle by a substitution. I have a variable that contains a number $results_tmp. I want to substitute it for the words in this scalar named $cli. $cli looks like this:

Code:
, ,sho bgp, , , , , , , ,

so if $results_tmp = 0.1 I want to substitute "0.1" for "sho bgp" so $cli will look like:

Code:
, ,0.1, , , , , , , ,

I have tried many variations of the following:

Code:
$cli = ", ,sho bgp, , , , , , , ,";
$results_tmp = "0.1";
$cli =~ s/($results_tmp)/^.*\w+.*$/;

This is my output from above:

Code:
Unrecognized escape \w passed through at ./bpBellSouthCoyote_Beta_10_4.pl line 158.
Final $ should be \$ or $name at ./bpBellSouthCoyote_Beta_10_4.pl line 158, within string
syntax error at ./bpBellSouthCoyote_Beta_10_4.pl line 158, near "=~ s/($results_tmp)/^.*\w+.*$/"
Execution of ./bpBellSouthCoyote_Beta_10_4.pl aborted due to compilation errors.

Any help is appriciated.

Nick




If at first you don't succeed, don't try skydiving.
 
Your regexp is backwards. You have the replacement pattern where the search pattern should be and vise-a-versa.

Maybe this will work:

Code:
$cli = ", ,sho bgp, , , , , , , ,";
$results_tmp = "0.1";
$cli =~ s/(?:\w+\s*\w*)/$results_tmp/;
print $cli;
 
your regexp wouldn't work as-is because the character class \w does not include spaces.
 
Thanks Kevin that worked.

What does the ?: do and does the regex only subsitute whats in the ()?

Also how could I handle it if there were 3 words in the string as in:

Code:
, , ,sho tunnel pear, , , , , ,

Thanks again.

If at first you don't succeed, don't try skydiving.
 
?: just means to not add the pattern into pattern memory ($1, $2,$3 etc). The pattern: \w+\s*\w* should work for any number of word/space combinations until a non word/non space character is found, like a comma. The problem would be for a pattern like this:

, , ,sho tunnel pear, , ,sho bgp , , ,

where there is more than one word/space combination between commas in the string. If there is always just one word/space combination then the substitution is simple and straight forward.

The regexp could be written without the ():

Code:
$cli = ", ,sho bgp, , , , , , , ,";
$results_tmp = "0.1";
$cli =~ s/\w+\s*\w*/$results_tmp/;
print $cli;

using the () just helped to group the pattern visually. It's really just a matter of preference in this case.






 
Thanks Kevin. Another star for you. There MAY be cases where there is more than one word/space combination. If I add the /g to the regex as in:

Code:
$cli =~ s/\w+\s*\w*/$results_tmp/g;

Would that take care of it?


If at first you don't succeed, don't try skydiving.
 
>> Would that take care of it?

It will if the real data is exactly like you have show in your sample data and you want to replace multiple occurances of \w+\s*\w* with $results_tmp
 
It is exactly like shown. Thanks

If at first you don't succeed, don't try skydiving.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top