473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

replace an expression inside multiline string

51
Hi, i'd like to be able to search and replace a substring within a multiline string.

for example, if i've got the following text.

[code]
struct xyz {
unsigned x;
unsigned y;
unsigned z;
}

struct abc {
unsigned x;
unsigned y;
unsigned z;
}
[\CODE]

i'd like to replace only the 'x' variable within the 'xyz' struct and NOT within the 'abc' struct, to 'm' variable (just for the example). in order to achieve my goal, i need to use the non-greedy matching, so that i won't catch the 'x' variable within the 'abc' struct.

i've tried to use the /m modifier so that '.' will match also a newline character.

[code]
perl -p -e 's/(struct xyz.*)unsigned x/$1unsigned m/m' test
[\CODE]

unfortunately, this doesn't work. perhaps you can help me figure out my mistake

thanks,
Mar 14 '11 #1
2 2823
rovf
41
How about non-greedy pattern matching? I.e. replace

.*

by

.*?

in your regexp.
Mar 14 '11 #2
toolic
70 Expert
This code does what you ask:

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. use File::Slurp;
  4.  
  5. my $file = read_file('./test');
  6. $file =~ s/(struct xyz.*?)unsigned x/$1unsigned m/s;
  7. print "$file\n";
You need to use s///s and ? to prevent greediness.
Mar 14 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Maxime Biais | last post by:
Hello, I heard that is possible to replace %(word) in a string with a dictionary containing { "word" : "replaced word" } with a function or a builtin from the python library. For example with...
1
by: John Dalberg | last post by:
I need to convert html into a multiline string which looks something like this: shtml = "<table border=""0"" cellspacing=""0"" cellpadding=""0"" width=""720"">" & _ "<tr><td width=""100%""...
8
by: Aussie Rules | last post by:
Hi, I have a string that is built on the fly, and often of different lengths. The string contains a series of numbers with a comma seperating each number. For example ...
7
by: Allerdyce.John | last post by:
Hi, I am new to python. I would like to know how to use python regular expression to substitute string value? I have an input string like this: x:11 y:0 w:760 h:19 area:14440 areaPerCent:0...
4
by: jpierson | last post by:
Hi, I'm having a few problems with the replace function for replacing characters in a textbox. "C:\" is the string i am tryin to remove ,with it a command I am sending does not work. ...
1
by: wizardRahl | last post by:
Hello, I'm looking to write a program that can replace words in a string given a user input. I need to replace the input (usually a sentence given by the user) to be less sexist. For ex. If...
11
by: Brian | last post by:
I have been working on a data reception system.. I am still finding my way around Javascript, though I am accomplishing much. I just fixed a flaw that was really hard to find. The symptoms are...
8
by: =?gb2312?B?zrrW0Luq?= | last post by:
I want to create a multiline string that can works in ie and firefox, who have a better idea ?
2
dmjpro
by: dmjpro | last post by:
I want to replace dot in a String. I used String.replaceAll ..but due to regular expression .... I couldn't find out the solution. I used String.replaceAll(".","something"); But in regular...
4
by: Danny Shevitz | last post by:
Simple question here: I have a multiline string representing the body of a function. I have control over the string, so I can use either of the following: str = ''' print state return True...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.