Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Parsing an Oracle connect string

by fbicknel (Beadle)
on Mar 04, 2011 at 22:00 UTC ( #891519=perlquestion: print w/replies, xml ) Need Help??
fbicknel has asked for the wisdom of the Perl Monks concerning the following question:

Fun stuff.

Say you have an oracle connect string that looks like:
['] [userid] [/ [password]] [@net_service_name] [']
Now say you want to parse that. I came up with this regex that seems to do it, at least for some of the simple tests below.

I humbly ask your opinions:
my ($userid, $password, $netServiceName) = /'?([^@\/']+)?(\/[^@\/']*)? +(@[^@\/']+)?'?/;

>>string<< is parsed as <userid> </password> <@net_service_name>

Note, btw, the special case of '/' which needs to parse as simply '/'.
>>SYS/change_on_install@prod1<< <SYS> </change_on_install> <@prod1> >>rman/rman@rcat<< <rman> </rman> <@rcat> >>SYS<< <SYS> <> <> >>/<< <> </> <> >>/fooey<< <> </fooey> <> >>/fooey@foobar<< <> </fooey> <@foobar> >>SYS@foobar<< <SYS> <> <@foobar> >>'simple/fooey@foobar'<< <simple> </fooey> <@foobar>

Replies are listed 'Best First'.
Re: Parsing an Oracle connect string
by a (Friar) on Mar 05, 2011 at 00:01 UTC
    Hmm, not really clear on what you've got here but:
    while (<DATA>) { chomp; my ($userid, $password, $net_service_name) = m#'?([^@/']+)?(/[^@/']+)?(@[^@/']+)?'?#; print "]]$_\[\[ \tis parsed as \[$userid] \[$password] [$net_service_name]\n" } # while __DATA__ >>SYS/change_on_install@prod1<< <SYS> </change_on_install> <@prod1> >>rman/rman@rcat<< <rman> </rman> <@rcat> >>SYS<< <SYS> <> <> >>/<< <> </> <> >>/fooey<< <> </fooey> <> >>/fooey@foobar<< <> </fooey> <@foobar> >>SYS@foobar<< <SYS> <> <@foobar> >>'simple/fooey@foobar'<< <simple> </fooey> <@foobar>
    I get a different result, seems you might want to consider white space, for one thing. That's not allowed, right? Are the pointy brackets legit Oracle chars?


      You're right: whitespace wasn't considered, I'll check to see if it's allowed. For now it's not. (edit: I checked.... it's allowed. I need to modify my r.e. to allow them then. Ick: quotes.) As to the pointy stuff... it's just a way to show each of the fields one by one. Sorry for the confusion.

      Ok, so I diddled a bit with this and came up with the following. It allows spaces in passwords (that's allowed, but not the other two fields.) It also allows spaces within the string, so long as they are next the delimiters, '/' and '@'. First, here's the code fragment:
      10 $string =~ s/ *([@\/]) */$1/g; 11 my ($userid, $diag, $password, $netServiceName) = $string =~ / +'?([^@\/']+)?(?:(\/)'?([^@\/']*)'?)?(@[^@\/']+)?'?/;
      Since I added the ability to surround the password in 'quotes', it unfortunately removed that bit from the '/' discovery. So that's now captured in $diag.

      Also (line 10) removes any optional spaces near the delimiters,
      . This seems to parse ok in my test program:
      SYS / foo_pw @ stuff SYS / ' foo_pw1' @ stuff SYS /' foo_pw2' @ stuff SYS / foo sp ace @ stuff SYS/change_on_install@prod1 LAST rman/rman@rcat SYS / /fooey /fooey@foobar SYS@foobar 'simple/fooey@foobar'
      Parses as:
      < SYS> < /foo_pw> [ @stuff] < SYS> < / foo_pw1> [ @stuff] < SYS> < / foo_pw2> [ @stuff] < SYS> < /foo sp ace> [ @stuff] < SYS> </change_on_i> [ @prod1] < LAST> < > [ ] < rman> < /rman> [ @rcat] < SYS> < > [ ] < > < /> [ ] < > < /fooey> [ ] < > < /fooey> [ @foobar] < SYS> < > [ @foobar] < simple> < /fooey> [ @foobar]
      (Note that my test program truncates some longer passwords in the printout.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://891519]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-20 13:49 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.