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

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\ConnPool\src\oraUtils.pc@@
..\Source\CPP\ConnPool\src\sample.pc@@
..\Source\CPP\Services\src\OExceptServer.pc@@
..\Source\CPP\Services\src\Rees1440Server.pc@@
..\Source\CPP\Services\src\SWDTestServer.pc@@
..\Source\CPP\TestCases\TestRees1800\testRees1800C ursor.pc@@
..\Source\CPP\TSRI\Remis1\cxxsrc\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\ConnPool\src\oraUtils.pc@@

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

Thanks.
EFP
Jul 19 '05 #1
3 2128
EFP wrote:
Sample Units.txt data:
.\Source\CPP\ConnPool\src\oraUtils.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\ConnPool\src\oraUtils.pc@@ (source)

Thanks


Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<3S********************@newsc.telia.net>...
EFP wrote:
Sample Units.txt data:
.\Source\CPP\ConnPool\src\oraUtils.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\ConnPool\src\oraUtils.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
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....
4
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...
11
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,...
18
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
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...
25
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...
4
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...
1
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...
1
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 "":...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.