473,810 Members | 3,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple Regular Expression.

EFP
Can anyone help me with a simple regular expression problem.

All that I want to do is take a list of known data and extract a
particular section of the string to form a new list.

Here is my code snipet:

my $fh = new FileHandle('c:\ Units.txt') ; ## sucks up my work file
my @lines1 = <$fh>; ## assigns the filehandler contents to @lines1
foreach $string (@lines1) ## extract each line from @lines and put it
in $string
{

$string =~ m/\*@@\$/;

print "NewString :: $string\n";
}

Sample Units.txt data:
..\Source\CPP\C onnPool\src\ora Utils.pc@@
..\Source\CPP\C onnPool\src\sam ple.pc@@
..\Source\CPP\S ervices\src\OEx ceptServer.pc@@
..\Source\CPP\S ervices\src\Ree s1440Server.pc@ @
..\Source\CPP\S ervices\src\SWD TestServer.pc@@
..\Source\CPP\T estCases\TestRe es1800\testRees 1800Cursor.pc@@
..\Source\CPP\T SRI\Remis1\cxxs rc\Buffer_Area. pc@@

I just want the:
oraUtils.pc

I'd even settle for:
oraUtils.pc@@

So basically I want the contents between "\" and the "@@"

I thought that:
$string =~ m/\*@@\$/;

Would go to the end of the search line and find "@@" and get anything
"*" between the "@@" and the "\" and set $string to this new value.

What it returns is:
element:: .\Source\CPP\Co nnPool\src\oraU tils.pc@@

Can anyone correct my understanding of regular expression and
assignment to a varibale.

Thanks.
EFP
Jul 19 '05 #1
3 2151
EFP wrote:
Sample Units.txt data:
.\Source\CPP\Co nnPool\src\oraU tils.pc@@
<snip>
I just want the:
oraUtils.pc
<snip>
So basically I want the contents between "\" and the "@@"

I thought that:
$string =~ m/\*@@\$/;

Would go to the end of the search line and find "@@" and get
anything "*" between the "@@" and the "\" and set $string to this
new value.
What on earth made you think that??
Can anyone correct my understanding of regular expression and
assignment to a varibale.


Yes, you can do that. By studying the Perl documentation for regular
expressions:

http://www.perldoc.com/perl5.8.0/pod/perlre.html

This would do what you want:

($string) = $string =~ /.*\\(.+)\@\@/;

But please use the docs to get an understanding of how it works.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #2
EFP
Based upon your expression I'm still not sure how it works. You have
the first ".*" which says 0 or more periods. Then you escape the slash
to look for a "\" then the "(.+)" match 1 or more periods and then you
escape both @@'s individually. So I still don't understand how the
".*" and the "(.+)" are used. Also, how does this say give me the
contents between the \ and the @@. I have read the reference pages
before and after this post. Could you shed some light on the
reasoning? I'm sure once you explain it regular expression will be
easier to comprehend.

..\Source\CPP\C onnPool\src\ora Utils.pc@@ (source)

Thanks


Gunnar Hjalmarsson <no*****@gunnar .cc> wrote in message news:<3S******* *************@n ewsc.telia.net> ...
EFP wrote:
Sample Units.txt data:
.\Source\CPP\Co nnPool\src\oraU tils.pc@@


<snip>
I just want the:
oraUtils.pc


<snip>
So basically I want the contents between "\" and the "@@"

I thought that:
$string =~ m/\*@@\$/;

Would go to the end of the search line and find "@@" and get
anything "*" between the "@@" and the "\" and set $string to this
new value.


What on earth made you think that??
Can anyone correct my understanding of regular expression and
assignment to a varibale.


Yes, you can do that. By studying the Perl documentation for regular
expressions:

http://www.perldoc.com/perl5.8.0/pod/perlre.html

This would do what you want:

($string) = $string =~ /.*\\(.+)\@\@/;

But please use the docs to get an understanding of how it works.

Jul 19 '05 #3
EFP wrote:
Gunnar Hjalmarsson wrote:
EFP wrote:

Sample Units.txt data:
.\Source\CPP\Co nnPool\src\oraU tils.pc@@

I just want the:
oraUtils.pc
($string) = $string =~ /.*\\(.+)\@\@/;


Based upon your expression I'm still not sure how it works. You
have the first ".*" which says 0 or more periods.


No it doesn't. '.' is a regex metacharacter that matches any character
(except newline) if it's not escaped, so it says 0 or more of _any_
characters.
Then you escape the slash to look for a "\"
Correct. Since the '.*' is "greedy", the '\\' matches the _last_
backslash. (Read about "greediness " in perldoc perlre.)
then the "(.+)" match 1 or more periods
Again, it matches 1 or more of _any_ characters. The parentheses
capture the content, and the regex returns it when it's evaluated in
list context. The parentheses surrounding $string enforces list
context. (Read about "context" in perldoc perldata.)
and then you escape both @@'s individually.


Yep.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1613
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own. The following code works for a single word: def createStandaloneWordRegex(word): """ return a regular expression that can find 'peter' only if it's written alone (next to space, start of string, end of string, comma, etc) but
4
3235
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
11
5398
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However, I want to use the text included within the brackets to do a lookup so that I can replace the expression with the new text. For example:
18
3047
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How ??
7
3834
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5180
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
4
2485
by: drasko | last post by:
Hi all. I need to code simple and fast int regexp_match(char *regexp, char *string) function that will follow the expression regexp, and see if there is a matching in the string. If there is, it will return 1 (TRUE). Are there some examples I can see, do you have ideas how to start with this, and so on... GNU C regexp is to big and complicated for me, it takes a lot of space (i need this for the use in embedded system). I'll...
1
4390
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find the first regular expression that matches the string. I've gor the regular expressions ordered so that the highest priority is first (if two or more regular expressions match the string I want the first one returned) The code that does this has...
1
3412
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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
10393
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,...
1
7664
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
6882
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.