Perl字符串解析 [英] Perl string parsing

查看:108
本文介绍了Perl字符串解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友...

我正在尝试在perl中解析此字符串.

I'm trying to parse this string in perl.

对于最后两节课,我正在测试Oracle tns文件并测试变体,如果代码确实需要花费一些时间,那么这个特别小,有人可以建议吗?

For last couple of classes I'm testing Oracle tns file and testing variations, this particular small if code is really taking time, could somebody please suggest?

基本上使用if语句,我试图读取tns文件并将单个值放入哈希引用中,直到if语句获得预期结果为止.

Basically with the if statement, I'm trying to read tns file and put single value in hash reference till if statement gets expected result...

  1. 从tns行获取单个别名值
  2. 获取该别名的SID
  3. 将不同的SId值放入哈希引用中,以便仅一次而不是多次连接数据库.

首先,如果可以,但是它可以多次连接几个数据库.

First if works but it connects couple of databases multiple times.

代码:

if /^(([A-Za-z][A-Za-z0-9]*)(\.([A-Za-z][A-Za-z0-9]*))*)(\s|,|=)/
{
  (/\(CONNECT_DATA\s+=\s+\(SID\s+=\s+(\w+\d+?)(\s+)?\)/)
    {  
      $hashref->{$1}="";
    }
}

样本文件(tnsfile.txt)

Sample file (tnsfile.txt)

DB1.UK, DB2.UK =
  (
    (ADDRESS = (PROTOCAL = TCP))
    (CONNECT_DATA = (SID = db1))
  )

DB1.EU, DB2.CH =
  (
    (ADDRESS = (PROTOCAL = TCP))
    (CONNECT_DATA = (SID = db1))
  )

DB3.UK =
  (
    (ADDRESS = (PROTOCAL = TCP))
    (CONNECT_DATA = (SID = db3))
  )

DB3.US =
  (
    (ADDRESS = (PROTOCAL = TCP))
    (CONNECT_DATA = (SID = db3))
  )

DB4.UK.US, DB5.US =
  (
    (ADDRESS = (PROTOCAL = TCP))
    (CONNECT_DATA = (SID = db5))
  )

预期结果:

DB1.Uk

DB3.UK

DB4.UK.US

推荐答案

正则表达式应为

/(^[A-Za-z0-9.]+)/

即一个字符串,锚定在包含一个或多个字母数字字符或点的行的开头.如果要更具体地忽略字符串开头或以点结束的行,则必须变得更加复杂,但是如果该文件是有效的TNS文件,则可能不需要这样做.

i.e. A string, anchored at the start of the line containing one or more alphanumeric characters or dots. If you want to get more specific about ignoring lines where the string starts or ends with a dot you'd have to get more complex, but that is probably not needed if the file is a valid TNS file.

这是一个更完整的正则表达式,仅匹配有效字符串

Here's a more complete regex that matches only valid strings

/^(([A-Za-z][A-Za-z0-9]*)(\.([A-Za-z][A-Za-z0-9]*))*)(\s|,|=)/

这篇关于Perl字符串解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆