Solved

Perl Parse String Question

Posted on 2009-07-13
9
276 Views
Last Modified: 2012-05-07
Hi,
I have a question on string regex in Perl. I have strings like

user/112312312789071820883/label/qwe
user/112312312789071820883/state/asdas/cvbv
user/-/state/asdas/cvbv

I would like to get third substrin, i.e. label or state in the above case. Hoe can I get that in Perl?

Thanks.
0
Comment
Question by:Purdue_Pete
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 13

Accepted Solution

by:
marchent earned 25 total points
ID: 24842695

$string = "user/112312312789071820883/state/asdas/cvbv";
print $string =~ m|^[^/]*/[^/]*/([^/]*)|;
 
 
$string = "user/112312312789071820883/label/qwe
user/112312312789071820883/state/asdas/cvbv
user/-/state/asdas/cvbv";
$\ = $/;
print for $string =~ m|^[^/]*/[^/]*/([^/]*)|msg;

Open in new window

0
 

Author Comment

by:Purdue_Pete
ID: 24842776
marchent,
Since it is a single string for each "user/....", I guess i will just replace m by s, right?

Can you explain what this line does in your code in detail?
print $string =~ m|^[^/]*/[^/]*/([^/]*)|;

K
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24842881

while(<DATA>) {
	my $third1 = (split/\//)[2];
	my ($third2) = m|.*?/.*?/(.*?)/|;
	
	print "third=$third1, $third2\n";
}
 
__DATA__
user/112312312789071820883/label/qwe
user/112312312789071820883/state/asdas/cvbv
user/-/state/asdas/cvbv

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 24843029
(split'/')[2]
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 24843063
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr|^[^/]*/[^/]*/([^/]*)|)->explain"
The regular expression:

(?-imsx:^[^/]*/[^/]*/([^/]*))

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  [^/]*                    any character except: '/' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  [^/]*                    any character except: '/' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  /                        '/'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^/]*                    any character except: '/' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
 
LVL 84

Expert Comment

by:ozo
ID: 24847166
neither /m nor /s is necessary
m just allows ^ and $ to match at the beginning and end of lines in addition to the beginning and end of the string,
s just allows . to match "\n"

my ($third2) = m|/.*?/(.*?)/|;
0
 

Author Comment

by:Purdue_Pete
ID: 24869883
marchent / ozo,
From this line,
print $string =~ m|^[^/]*/[^/]*/([^/]*)|;

I do get 'state' or 'label'. How can I store that in a variable?

K
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24870457

my $third = $string =~ m|^[^/]*/[^/]*/([^/]*)|;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24872137
my ($third) = $string =~ m|/[^/]*/([^/]*)|;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question