Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex help please :( 2

Status
Not open for further replies.

perlone

Programmer
May 20, 2001
438
US
Hello,

I have some image files that has names like:

PC-blah1.jpg
GBA-test.jpg

Is there anyway I can get the value 'blah1' and 'test'? I need to get the value between the '-' and '.jpg' . If you can, please help and thank you so much for reading.

-Aaron
 
When you're doing your Regex, you can use the following Variables to grab what you're looking for. It won't be pretty, but it will work.

Code:
$` - Returns everything before the match
$' - Returns everything after the match
$& - Returns only the match
$+ - Returns the last paranthetical match

Hope this helps!

- Rieekan
 
I'd be tempted to use the more familiar
Code:
   my @names = qw{
      PC-blah1.jpg
      GBA-test.jpg
   };

   foreach ( @names ) {
      /[^-]*-([^.]+)\./ && print "$_ -> $1\n";
   }

This means

Code:
[^-]*
match any character which is not a minus any number of times
Code:
-
match a minus sign
Code:
(
start collecting into $1
Code:
[^.]+
match anything other than a period one or more times
Code:
)
stop collecting into $1
Code:
\.
match a literal period

Hope this helps,

Yours,


fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Using Rieekan's approach above:
[tt]
$_="PC-blah1.jpg";
/-/; # find the /
$_=$'; # save everything after it
/\./; # find the .
$_=$`; # save everything before it
print; # print result
[/tt]


Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top