473,385 Members | 1,320 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,385 software developers and data experts.

Regex for matching over 2 lines

25
Hi,
I have thousands of lines of code like this:
Expand|Select|Wrap|Line Numbers
  1. Blah1 blah1 blah1 = A1;
  2. Yada1 yada1 yada1 = B1;
  3. Blah2 blah2 blah2 = A2;
  4. Yada2 yada2 yada2 = B2;
I want to swap the A value with the B value.

What is the syntax for a Perl regex that would do this? I'm struggling with the line break.

Thanks in advance.

svend
Feb 25 '11 #1

✓ answered by Rabbit

Assuming you have multi-line turned on, \r\n represents a line break in windows while \n represents a line break in unix.

5 2365
Rabbit
12,516 Expert Mod 8TB
Assuming you have multi-line turned on, \r\n represents a line break in windows while \n represents a line break in unix.
Feb 25 '11 #2
svendok
25
Thank you! I can't believe I forgot that...

So I guess the patterns would be:
Search:
Expand|Select|Wrap|Line Numbers
  1. (.*) = (.*)\r\n(.*) = (.*)
Replace:
Expand|Select|Wrap|Line Numbers
  1. $1 = $4\r\n$3 = $2
Feb 25 '11 #3
Rabbit
12,516 Expert Mod 8TB
If you have trouble with the "." you may want to try ([^\r\n]*)
Feb 25 '11 #4
miller
1,089 Expert 1GB
perl automatically treats \n as \r\n internally on windows machines, so it is unnecessary to do that. At most, you might need to do \r?\n if you're working with files cross platform, but I wouldn't expect that you are.

Anyway, here's a script that switches the values of every odd and even line:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. my $data = do {local $/; <DATA>};
  4.  
  5. $data =~ s{(.*?) = (.*?)\n(.*?) = (.*?)\n}{$1 = $4\n$3 = $2\n}g;
  6.  
  7. print $data;
  8.  
  9. __DATA__
  10. Blah1 blah1 blah1 = A1;
  11. Yada1 yada1 yada1 = B1;
  12. Blah2 blah2 blah2 = A2;
  13. Yada2 yada2 yada2 = B2;
  14. Blah3 blah3 blah3 = A3;
  15. Yada3 yada3 yada3 = B3;
  16.  
Feb 25 '11 #5
svendok
25
Thanks, Miller, that's very helpful! Didn't know that about Perl and Windows line breaks.
Mar 18 '11 #6

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

Similar topics

1
by: hendry | last post by:
I am trying to grab the lines from a textarea that match a substring. How best to implement this? I tried regex and failed miserably. Here I try to match every line with INFO:...
5
by: Bob | last post by:
I think this is very simple but I am having difficult doing it. Basically take a comma separated list: abc, def, ghi, jk A list with only one token does not have any commas: abc The first...
1
by: jason | last post by:
The following vb code displays every line that has a particular string in it. How do I list every line that does not have that string. "!" does not seem to work for me and not sure where to place...
1
by: George Durzi | last post by:
Consider this excerpt from some HTML. (This is a copy from View->Source, except for the comment) <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0> <?xml version="1.0" encoding="UTF-16"?>...
2
by: George Durzi | last post by:
Consider the following HTML snippet. I want to extract the section shown below. <!-- some html --> <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 border=0><?xml version=1.0 encoding=UTF-16?> **...
3
by: Edward K. Ream | last post by:
Here is a little regular expression puzzle. I wish a regex that matches an rst code block, that is, '::' followed by a group of lines all indented by at least as much as the first non-blank...
0
by: Tidane | last post by:
Visual Basic.NET Framework 2.0 I've created a program to parse out text as the program recieved it and use Regex matching to decide what should be done. My problem is that the text is matching when...
10
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 ...
3
by: jwwest | last post by:
Anyone have any trouble pattern matching on lines returned by readline? Here's an example: string = "Accounting - General" pat = ".+\s-" Should match on "Accounting -". However, if I read...
0
by: jagged | last post by:
I'm new to C# so am not completely familiar with its regular expression flavor and I don't understand why I can't get this to work. I set the singline | ignorecase options in the regex ctor. Given...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.