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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx - extracting data

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
0
0
GB
Dear all,

I need to be able to extract data from a variable. An example variable is shown

/opt/asdfgwdq/dasdasd/Data_to_be_extracted_is here

The data that I need to be extracted should be from the right most character (alpha-numeric) to the last letter (alpha-numeric) before the last forward slash "/".

In the above case the data I am after is

"Data_to_be_extracted_is here"


I tried the following :

$element_name = ( element_name =~ /.*\/(.*)$/);

Any help would be appreciated.

Thanks
 
Code:
($element_name) = $element_name =~ |/(.*)$|;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Choose any of the following. I suspect that the last is the one that you are actually after:

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$element_name[/blue] = [red]'[/red][purple]opt/asdfgwdq/dasdasd/Data_to_be_extracted_is here[/purple][red]'[/red][red];[/red]
[blue]$element_name[/blue] =~ [red]s{[/red][purple].*/(.*)[/purple][red]}[/red][red]{[/red][purple][blue]$1[/blue][/purple][red]}[/red][red];[/red]
[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Search and Replace Regex: '[blue]$element_name[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$element_name[/blue] = [red]'[/red][purple]opt/asdfgwdq/dasdasd/Data_to_be_extracted_is here[/purple][red]'[/red][red];[/red]
[red]([/red][blue]$element_name[/blue][red])[/red] = [red]([/red] [blue]$element_name[/blue] =~ [red]m{[/red][purple]([^/]*)$[/purple][red]}[/red][red])[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]Matching Regex: '[blue]$element_name[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]File::Basename[/green] [red]qw([/red][purple]fileparse[/purple][red])[/red][red];[/red]
[black][b]my[/b][/black] [blue]$element_name[/blue] = [red]'[/red][purple]opt/asdfgwdq/dasdasd/Data_to_be_extracted_is here[/purple][red]'[/red][red];[/red]
[blue]$element_name[/blue] = [maroon]fileparse[/maroon][red]([/red][blue]$element_name[/blue][red])[/red][red];[/red]
[black][b]print[/b][/black] [red]"[/red][purple]File name parsing: '[blue]$element_name[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Core (perl 5.8.8) Modules used :
[ul]
[li]File::Basename - Parse file paths into directory, filename and suffix.[/li]
[/ul]
[/tt]

- Miller
 
hello KevinADC (TechnicalUser)

I tried this but couldn't work why the alternation "|" was used in this case. I understand the use of alternation as (sadas|asas|sdas|).

Thanks

MillerH (Programmer)

Thanks, the option 3 did largely what i needed.

thanks all

Alf

 
hello KevinADC (TechnicalUser)

I tried this but couldn't work why the alternation "|" was used in this case. I understand the use of alternation as (sadas|asas|sdas|).
You can use a normal '/' limiter as well. I guess KevinADC used '|' because then you don't have to escape '/' in a regex.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
spookie,

that is correct about why I used '|'. But I see I made a mistake because if you use a different delimiter in a regexp besides the default forward slash '/', you should add the operator before the regex, in this case the 'm':

Code:
$element_name = '/opt/asdfgwdq/dasdasd/Data_to_be_extracted_is here';
($element_name) = $element_name =~ [red]m[/red]|.*/(.*)$|;
print $element_name;





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
KevinADC,
Good catch :)



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Couldn't you also take your variable and switch the "/" with a "comma" and then pop off the last field?

if element_name is
/opt/asdfgwdq/dasdasd/Data_to_be_extracted_is here

then ...

@data = split /\//, $element_name;
$Data_to_be_extracted = pop @data;

Just a thot.
 
lchenard,

If one is doing filename parsing, just let the File::Basename module do everything for you. This way your code isn't OS specific.

But yes, split is just another method of using a regex.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top