473,785 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

replace an expression inside multiline string

51 New Member
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 2837
rovf
41 New Member
How about non-greedy pattern matching? I.e. replace

.*

by

.*?

in your regexp.
Mar 14 '11 #2
toolic
70 Recognized Expert New Member
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
3015
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 the string: "hello, %(word1) %(word2) !" and the dictionary: {"word1" : "python", "word2" : "lovers"} the final string would be: "hello python lovers"
1
1869
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%"" valign=""top""><br><!-- Main Table -->" & _ .... Is there a tool that can do this?
8
1560
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 '2,4,7,8,12,45,67,8,9,33' or it could be '2,6,9' The problem I have is that i am passing this into a stored procedure 'in'
7
3177
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 totalAreaPerCent:-3.08011e+16 type:3 path:///-/1/1 and I would like to convert it to: rect x="11" y="0" width="760" height="14440"
4
14532
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. txtS_Filename.Text.Replace("C:\", ""); The compiler has a problem with the newline slash character but even
1
2037
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 the user input is : " I like the way he does that" The program should output " I like the way he or she does that" .... something to that effect. Anyone know any good links or topics here that I could get an idea on the script replacement part? ...
11
5085
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 this: I get a multiline string returned to Javascript from a Proxy+Google Maps API GDownloadUrl() The data, when added to a DOM table looked fine, about 20 lines in CSV format
8
8027
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
41196
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 expression "." has a special meaning ..... Debasis Jana
4
2130
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
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.