`
sco.struts
  • 浏览: 18904 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Everyday Scripting with Ruby 读书笔记(5)

    博客分类:
  • Ruby
阅读更多
---
用正则表达式刮取网页
Scraping Web Pages with Regular Expressions
---


irb(main):001:0> require 'open-uri'
=> true
① open方法可以打开文件和URL
irb(main):002:0> page = open('http://espn.go.com')
=> #<File:C:/DOCUME~1/GRAY~1.LAN/LOCALS~1/Temp/open-uri.2544.0>
② 同行两条语句只打印最后一个输出
irb(main):003:0> text = page.read; nil
=> nil
③ scan方法返回匹配给定正则表达式的所有子串,有分组的话每个元素本身都是数组
④ 问号可以避免“贪婪”

irb(main):014:0> text.scan(/<li\s+class=\"(lo.*?)\".*><a.+>(.+)<\/a>/)
=> [["lo", "ESPN"], ["lo", "Fantasy"], ["lo", "NFL"], ["lo solid", "MLB"], ["lo solid", "NBA"], ["lo solid", "NHL"], ["lo solid", "ESPNU"], ["lo solid", "College FB"], ["lo solid", "Men's BB"], ["lo solid", "Women's BB"], ["lo solid", "NASCAR"], ["lo solid", "Racing"], ["lo solid", "Golf"], ["lo solid", "Soccer"], ["lo solid", "High School"], ["lo solid", "Tennis"], ["lo solid", "Boxing"], ["lo solid", "More +"]]

散列表
irb(main):002:0> hash = {}
=> {}
irb(main):003:0> hash[:firstname] = "George"
=> "George"
irb(main):004:0> hash[:lastname] = "Hamilton"
=> "Hamilton"
irb(main):005:0> hash[:firstname]
=> "George"
irb(main):006:0> hash2 = {:firstname => "Tom", :lastname => "Johnson"}
=> {:firstname=>"Tom", :lastname=>"Johnson"}
irb(main):007:0> hash
=> {:firstname=>"George", :lastname=>"Hamilton"}
irb(main):008:0>

字符串截取
irb(main):008:0> "abcdefg"[0..2]
=> "abc"
irb(main):009:0> "abcdefg"[0...2]
=> "ab"
分享到:
评论

相关推荐

    everyday scripting with ruby

    everyday scripting with ruby

    Everyday Scripting With Ruby

    《Everyday Scripting with Ruby》是一本综合性的Ruby教程,涵盖了从基础知识到高级技术的各个方面。通过实践导向的教学方法,读者不仅可以学习到Ruby的核心概念,还能掌握如何在真实环境中应用这些知识。无论是初学...

    Scripting With AD Scripting With AD

    标题 "Scripting With AD" 暗示了我们即将探讨的是使用脚本语言与Active Directory(AD)进行交互的主题。Active Directory是微软Windows操作系统中的一个关键组件,它用于存储和管理网络资源,如用户账户、计算机...

    Unreal Engine 4 Scripting with C++ Cookbook first edition.pdf

    Unreal Engine 4 Scripting with C++Cookbook Get the best out of your games by scripting them using UE4 William Sherif Stephen Whittle 2016版

    Unreal Engine 4.x scripting with C cookbook

    Unreal Engine 4.x scripting with C cookbook develop quality game components and solve scripting problems with the power of C and UE4 by Stephen Whittle John P. Doran William Sherif (z-lib.org)

    Unreal Engine 4 Scripting with C++.pdf

    标题《Unreal Engine 4 Scripting with C++》指明了这本书是关于如何使用C++语言进行Unreal Engine 4(UE4)游戏引擎的脚本编程。UE4是Epic Games开发的一款先进的游戏引擎,广泛应用于现代游戏开发中,它支持C++...

    Unreal Engine 4 Scripting with C++ Cookbook

    Unreal Engine 4 Scripting with C++ Cookbook 2016 | ISBN-10: 1785885545 | 431 pages | PDF | 7 MB Key Features A straightforward and easy-to-follow format A selection of the most important tasks and ...

    Unreal Engine 4 Scripting with C++ Cookbook - 2016.pdf (带彩色插图)

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本面向游戏开发者的专业书籍,专门介绍了如何使用C++语言结合Unreal Engine 4(UE4)进行游戏脚本编写。本书在2016年10月发布了新版,并且包含彩色插图,使得...

    Unreal Engine 4 Scripting with C++ Cookbook willam

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本专为游戏开发人员设计的实用指南,旨在帮助读者深入理解并掌握使用C++在Unreal Engine 4(UE4)中进行脚本编程的技术。这本书涵盖了从基础概念到高级技巧的...

    Unreal Engine 4 Scripting with C++ Cookbook pdf 0分

    标题和描述中提到的《Unreal Engine 4 Scripting with C++ Cookbook》是一本关于Unreal Engine 4(UE4)游戏引擎中使用C++编程语言进行脚本编程的实用指导书。这本书以食谱(Cookbook)的形式呈现,为读者提供了许多...

    Raven Scripting Java Builds With Ruby

    CHAPTER 2 Raven Takes off...............................5 CHAPTER 3 Wait, I Have Dependencies!....................19 CHAPTER 4 Divide and Conquer: Multimodule Projects......33 CHAPTER 5 Public or ...

    Advanced Bash-Scripting Guide 读书笔记

    在阅读《Advanced Bash-Scripting Guide》这本书的过程中,我们能学到许多有关Bash脚本的高级用法和技巧。这本书对于那些想要提升其Bash脚本编写能力的用户来说是一份宝贵的资料。接下来,我将根据给定文件的部分...

    Linux Shell Scripting with Bash

    ### Linux Shell Scripting with Bash #### 核心知识点解析 **1. Linux Shell Scripting 基础** - **Shell 的概念与作用** - Shell 是一个命令解释器,是用户与操作系统之间的交互界面。 - 用户通过输入命令,...

    Microsoft - Windows Scripting With Wmi(2007)

    《Microsoft - Windows Scripting With Wmi(2007)》是关于Windows Management Instrumentation(WMI)技术的一本PDF教程,旨在帮助IT专业人士深入理解和掌握利用WMI进行Windows脚本编程的技术。WMI是微软提供的一种...

    Unreal Engine 4 Scripting with C Cookbook (Will.pdf ) 彩色高清

    《Unreal Engine 4 Scripting with C++ Cookbook》是一本为游戏开发者准备的实用指南,它教授如何利用C++在虚幻引擎4环境下进行高级脚本编写。通过这本书,开发者可以学习到如何优化游戏性能、实现复杂的游戏逻辑...

Global site tag (gtag.js) - Google Analytics