Solved

Perl Parse String Question

Posted on 2009-07-13
9
271 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
 
LVL 84

Expert Comment

by:ozo
ID: 24843029
(split'/')[2]
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now