Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

regexp help

Posted on 2006-05-17
13
Medium Priority
?
329 Views
Last Modified: 2010-03-05
Ok, I have been beating this one around for a few days, and just cant get it.
I need a regexp that will match the line below,

<Item name="IP Filter Disallowed" type="string">221.141.0.194 221.141.*.*</Item>

and capture everything after string">
and before </item>

my last try (with testing) was looking at the line
<Item name="IP Filter Disallowed" type="string">221.141.0.194</Item>

with using
$FXline = ~/(<Item name="IP Filter Disallowed" type="string">)([0-9]{1,3}(\.[0-9]|[*]{1,3}){3})(<\/Item>)/;

but $2 is always "" ;(

Thanks...





Thanks
0
Comment
Question by:ghboom
  • 3
  • 3
  • 3
  • +2
11 Comments
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 16705002
You seem to be trying to match too many bits of the string in pattern memory, if all you need is 221.141.0.194 part of the string you can do something like this:

my $FXline = '<Item name="IP Filter Disallowed" type="string">221.141.0.194</Item>';
my ($match) = $FXline =~ /<Item name="IP Filter Disallowed" type="string">([\d.]+)<\/Item>/;
print $match;
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 16705165
Or your parenthesis are screwing your regex up.  Try:

$FXline = ~/(<Item name="IP Filter Disallowed" type="string">)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)(<\/Item>)/;

and see what $2 has then...
0
 

Author Comment

by:ghboom
ID: 16705248

perldiver,
the reason is Im searching for the whole line is that its part of a much bigger picture,
I only want results from THAT line ...

I just did

 ~/(<Item name="IP Filter Disallowed" type="string">)(.*)(<\/Item>)/)

and it finaly worked :)

thanks anyways ! ;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:ozo
ID: 16705280
if $FXline can contain more than one item, it would be better to use
/(<Item name="IP Filter Disallowed" type="string">)(.*?)(<\/Item>)/
0
 
LVL 85

Expert Comment

by:ozo
ID: 16705303
perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/(<Item name="IP Filter Disallowed" type="string">)([0-9]{1,3}(\.[0-9]|[*]{1,3}){3})(<\/Item>)/)->explain'
The regular expression:

(?-imsx:(<Item name="IP Filter Disallowed" type="string">)([0-9]{1,3}(\.[0-9]|[*]{1,3}){3})(</Item>))

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):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    <Item name="IP           '<Item name="IP Filter Disallowed"
    Filter Disallowed"       type="string">'
    type="string">
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [0-9]{1,3}               any character of: '0' to '9' (between 1
                             and 3 times (matching the most amount
                             possible))
----------------------------------------------------------------------
    (                        group and capture to \3 (3 times):
----------------------------------------------------------------------
      \.                       '.'
----------------------------------------------------------------------
      [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [*]{1,3}                 any character of: '*' (between 1 and 3
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
    ){3}                     end of \3 (NOTE: because you're using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \3)
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  (                        group and capture to \4:
----------------------------------------------------------------------
    </Item>                  '</Item>'
----------------------------------------------------------------------
  )                        end of \4
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
 
LVL 85

Expert Comment

by:ozo
ID: 16705331
maybe you meant
([0-9]{1,3}([.\s]([0-9]{1,3}|[*]))*)
although
(.*?)
may suffice
You don't need parentheses around
(<Item name="IP Filter Disallowed" type="string">)
and
(<\/Item>)
in order to match them
0
 

Author Comment

by:ghboom
ID: 17067811

jmcq  
on 5/17 I replied that I had found my solution....
I guess that entitles me to a refund ?

GHBoom
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17072208
Well, the problem seems to have been the parenthesis, as I pointed out.  You just took what I suggested and replaced ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) with (.*)...
0
 

Author Comment

by:ghboom
ID: 17094485
mjcoyne ,
with all due respect, (.*) was what I found worked without even seeing your responce.
I found the program "The Regex Coach" used it, found my solution...

With quite abit less respect,
I came here for help, how would taking your code, changing it and responding with my solution
benifit me in any way ?
I didnt bother to refresh the page after reading Perl_Divers solution.
Next time think about all the possibilitied before, implying someone stole your idea and
reducing your reputation.

GHBoom


0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 17102534
Your question was, essentially,

[Why] $2 is always ""

My solution gave you the answer to that question -- $2 was "" because you had your capturing parenthesis screwed up.  Perl_Diver's response did not solve your problem, as you acknowledged in your response to him.

You went from "$FXline = ~/(<Item name="IP Filter Disallowed" type="string">)([0-9]{1,3}(\.[0-9]|[*]{1,3}){3})(<\/Item>)/;", in which $2 was null, to "$FXline = ~/(<Item name="IP Filter Disallowed" type="string">)(.*)(<\/Item>)/;", in which $2 works, because you've re-arranged your parenthesis as I suggested.

I couldn't care less about the points -- give them to Perl_Diver or ozo, if you like -- and I have no worries about my reputation, thanks.  I just think asking for a refund, given the circumstances, is a bit much.

No worries, whatever the admin feels is appropriate is okay with me.
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 17118338
Closed, 250 points refunded.
Netminder
Site Admin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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 …
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…
Six Sigma Control Plans
Suggested Courses

572 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