加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ruby使用正则表达式从字符串中提取数据

发布时间:2020-12-16 20:02:11 所属栏目:百科 来源:网络整理
导读:我正在做一些web抓取,这是数据的格式 Sr.No. Course_Code Course_Name Credit Grade Attendance_Grade 我收到的实际字符串具有以下形式 1 CA727 PRINCIPLES OF COMPILER DESIGN 3 A M 我感兴趣的是Course_Code,Course_Name和Grade,在这个例子中,值将是 Cours
我正在做一些web抓取,这是数据的格式
Sr.No.  Course_Code Course_Name Credit  Grade   Attendance_Grade

我收到的实际字符串具有以下形式

1 CA727 PRINCIPLES OF COMPILER DESIGN 3 A M

我感兴趣的是Course_Code,Course_Name和Grade,在这个例子中,值将是

Course_Code : CA727
Course_Name : PRINCIPLES OF COMPILER DESIGN
Grade : A

有一些方法可以使用正则表达式或其他一些技术来轻松地提取此信息,而不是手动解析字符串.
我在1.9模式下使用jruby.

解决方法

让我们用Ruby的命名捕获和一个自我描述的正则表达式!
course_line = /
    ^                  # Starting at the front of the string
    (?<SrNo>d+)       # Capture one or more digits; call the result "SrNo"
    s+                # Eat some whitespace
    (?<Code>S+)       # Capture all the non-whitespace you can; call it "Code"
    s+                # Eat some whitespace
    (?<Name>.+S)      # Capture as much as you can
                       # (while letting the rest of the regex still work)
                       # Make sure you end with a non-whitespace character.
                       # Call this "Name"
    s+                # Eat some whitespace
    (?<Credit>S+)     # Capture all the non-whitespace you can; call it "Credit"
    s+                # Eat some whitespace
    (?<Grade>S+)      # Capture all the non-whitespace you can; call it "Grade"
    s+                # Eat some whitespace
    (?<Attendance>S+) # Capture all the non-whitespace; call it "Attendance"
    $                 # Make sure that we're at the end of the line now
/x

str = "1   CA727   PRINCIPLES OF COMPILER DESIGN   3   A   M"
parts = str.match(course_line)

puts "
Course Code: #{parts['Code']}
Course Name: #{parts['Name']}
      Grade: #{parts['Grade']}".strip

#=> Course Code: CA727
#=> Course Name: PRINCIPLES OF COMPILER DESIGN
#=>       Grade: A

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读