找回密码
 加入
搜索
查看: 14637|回复: 22

[系统综合] 求正则一例

 火.. [复制链接]
发表于 2014-7-15 23:05:33 | 显示全部楼层 |阅读模式
本帖最后由 sex123 于 2014-7-15 23:09 编辑

http://www.ncbi.nlm.nih.gov/pubmed/24755572
http://www.ncbi.nlm.nih.gov/pubmed/24406157
http://www.ncbi.nlm.nih.gov/pubmed/24753396
这些网址右上角均有一个图标,网页源代码都有icon for的字样。最好是根据icon for写正则表达式,
另外,下面的这些没有图标
http://www.ncbi.nlm.nih.gov/pubmed/30
http://www.ncbi.nlm.nih.gov/pubmed/300
http://www.ncbi.nlm.nih.gov/pubmed/354234
这样的网页就不需要处理正则了。
网页url地址都是这样的
http://www.ncbi.nlm.nih.gov/pubmed/$pmid
$pmid一定为数值。
第一步,输入任一$pmid值
第二步,读取网页http://www.ncbi.nlm.nih.gov/pubmed/$pmid
第三步,判断网页是否有图标,即源代码是否有icon for字样
第四步,如果没有图标,则程序直接结束
第五步,如果有图标,则通过正则取以下信息,如果是一个网页出现多个图标,只分析第一个图标即可。
以这个网址为例:
http://www.ncbi.nlm.nih.gov/pubmed/24755572
需要得到以下信息,这些源代码就是图标源代码附近,一般是在上面一行。
journal="PLoS One" 即 $journal = "PLoS One"
free_status="free"  即 $free_status=True 注意,有的网页没有这个free_status="free" ,则 $free_status=False
href="http://dx.plos.org/10.1371/journal.pone.0094898" 即 $href ="http://dx.plos.org/10.1371/journal.pone.0094898"
alt="Icon for Public Library of Science" 即$alt="Icon for Public Library of Science"
第六步,如果有图标,点击LinkOut - more resources得到展开有链接这些附加信息,在网页下部有LinkOut - more resources可以展开,即
    Public Library of Science
    Europe PubMed Central
    PubMed Central
    PubMed Central Canada
这些展开的网页文字和对应的链接,可以写入数组形式,或者是普通的变量,即
$Linkout[1,1]="Public Library of Science"
$Linkout[1,2]="http://dx.plos.org/10.1371/journal.pone.0094898"
$Linkout[2,1]="Europe PubMed Central"
$Linkout[2,2]="http://europepmc.org/abstract/MED/24755572"
.....
$Linkout[4,1]=....
$Linkout[4,2]=...
谢谢。
发表于 2014-7-16 09:23:04 | 显示全部楼层
好像你这个不需要正则就可以处理,直接用 StringInStr 试一试
发表于 2014-7-16 12:59:45 | 显示全部楼层
本帖最后由 shqf 于 2014-7-16 13:03 编辑

既然是想用正则解决,就只要告诉源字符串内容及想要获取的字符串内容就行了。
 楼主| 发表于 2014-7-16 20:16:12 | 显示全部楼层
要这一块的内容提取。
<div>

    <div class="icons">
        <a target="_blank" journal="PLoS One" free_status="free" ref="PrId=4656&itool=Abstract-def&uid=24755572&nlmid=101285081&db=pubmed&log$=linkouticon&ftext_icon_expt=fulltext3" href="http://dx.plos.org/10.1371/journal.pone.0094898">
            <img border="0" src="//www.ncbi.nlm.nih.gov/corehtml/query/egifs/http:--www.plosone.org-images-pone_120x30.png" title="Read full text in Public Library of Science" alt="Icon for Public Library of Science"></img>
        </a>
        <a target="_blank" journal="PLoS One" free_status="free" ref="PrId=3494&itool=Abstract-nondef&uid=24755572&nlmid=101285081&db=pubmed&log$=linkouticon&ftext_icon_expt=fulltext3" href="http://www.ncbi.nlm.nih.gov/pmc/articles/pmid/24755572/"></a>
    </div>
 楼主| 发表于 2014-7-18 12:08:53 | 显示全部楼层
顶起, 为什么没人帮助呢?
发表于 2014-7-18 13:04:50 | 显示全部楼层
整个这段内容要提取出来,还是这一段中的某些内容要提取? 如果是后者,那么具体提取什么内容、哪些内容能说清楚吗?
不说清楚,没人能帮你,没法帮你呵。
发表于 2014-7-18 13:11:11 | 显示全部楼层
正则在你的帖子中不占主导地位~
 楼主| 发表于 2014-7-18 14:10:16 | 显示全部楼层
要这一块的内容提取。
<div>

    <div class="icons">
        <a target="_blank" journal="PLoS One" free_status="free" ref="PrId=4656&itool=Abstract-def&uid=24755572&nlmid=101285081&db=pubmed&log$=linkouticon&ftext_icon_expt=fulltext3" href="http://dx.plos.org/10.1371/journal.pone.0094898">
            <img border="0" src="//www.ncbi.nlm.nih.gov/corehtml/query/egifs/http:--www.plosone.org-images-pone_120x30.png" title="Read full text in Public Library of Science" alt="Icon for Public Library of Science"></img>
        </a>
        <a target="_blank" journal="PLoS One" free_status="free" ref="PrId=3494&itool=Abstract-nondef&uid=24755572&nlmid=101285081&db=pubmed&log$=linkouticon&ftext_icon_expt=fulltext3" href="http://www.ncbi.nlm.nih.gov/pmc/articles/pmid/24755572/"></a>
    </div>
要里面的
PLoS One
free
http://dx.plos.org/10.1371/journal.pone.0094898
Icon for Public Library of Science 或 Read full text in Public Library of Science
要通过正则取得这四个关键的变化内容
发表于 2014-7-18 14:54:25 | 显示全部楼层
(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>
发表于 2014-7-18 15:18:03 | 显示全部楼层
本帖最后由 shqf 于 2014-7-18 15:19 编辑

A大的正则总那么精简,学习了。我的代码长了点,献丑了,仅供楼主参考:
(?i)(?s)journal="(.+?)" free_status="(.+?)".+href="(.+?)".+alt="(.+?)"></img>
 楼主| 发表于 2014-7-18 17:29:55 | 显示全部楼层
afan 发表于 2014-7-18 14:54



   


#include<array.au3>
Local $html=BinaryToString(InetRead("http://www.ncbi.nlm.nih.gov/pubmed/24755572",1),1)
;$html=StringReplace($html,@CRLF,"")

;Local $iconUrl=StringRegExp($html,'<a href="([^"]+).+?Icon for', 3)
;Local $iconJournal=StringRegExp($html,'journal="([^"]+).+?Icon for', 3)
;Local $iconFree=StringRegExp($html,'free_status="([^"]+).+?Icon for', 3)
;Local $iconFor=StringRegExp($html,'title="([^"]+).+?Icon for', 3)
;Local $iconFor[0]=StringRegExp($html,'Icon for.+?', 3)
;MsgBox(0,0,$iconFor[0])
;Local $iconFor2=StringReplace($iconFor[0],"Read full text in ","")
;Local $iconAll=$iconUrl[0]&@CRLF&$iconJournal[0]&@CRLF&$iconFor2

;If Not @error Then MsgBox(0, '', $iconAll)
Local $iconFor=StringRegExp($html,'(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>', 3)
_ArrayDisplay($iconFor)

;(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>


为什么没有反应就退出了呢?
发表于 2014-7-18 17:42:15 | 显示全部楼层
回复 11# sex123


    根本没有你贴出的示例字符串
 楼主| 发表于 2014-7-18 18:46:01 | 显示全部楼层
回复  sex123


    根本没有你贴出的示例字符串
afan 发表于 2014-7-18 17:42



    不会吧,网址也对呀,是什么原因造成的呢?
 楼主| 发表于 2014-7-18 21:22:42 | 显示全部楼层
自已学着写了写,不知道这样对不对?

#cs ----------------------------------------------------------------------------
        
        AutoIt Version: 3.3.12.0
        Author:         myName
        
        Script Function:
        Template AutoIt script.
        
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include<array.au3>
Local $html = BinaryToString(InetRead("http://www.ncbi.nlm.nih.gov/pubmed/24753396", 1), 1)
;$html=StringReplace($html,@CRLF,"")

Local $iconUrl = StringRegExp($html, '<a href="([^"]+).+?Icon for', 3)
If Not @error Then MsgBox(0, '', $iconUrl[0])
Local $iconJournal = StringRegExp($html, 'journal="([^"]+).+?Icon for', 3)
If Not @error Then MsgBox(0, '', $iconJournal[0])
Local $iconFree = StringRegExp($html, 'free_status="([^"]+).+?Icon for', 3)
#cs
        If @error Then
        $iconFree[0]=False
        MsgBox(0, '', $iconUrl[0])
        Else
        $iconFree[0]=True
        MsgBox(0, '', $iconUrl[0])
        EndIf
#ce
If Not @error Then
        MsgBox(0, '', $iconFree[0])
        $iconFreeflag = True
Else
        $iconFreeflag = False
EndIf
MsgBox(0, '', $iconFreeflag)
;Local $iconFor=StringRegExp($html,'title="([^"]+).+?Icon for', 3)
Local $iconFor = StringRegExp($html, '(?<=Icon for )[\w\h]*', 3)
If Not @error Then MsgBox(0, '', $iconFor[0])
;MsgBox(0,0,$iconFor[0])
;Local $iconFor2=StringReplace($iconFor[0],"Read full text in ","")
;Local $iconAll=$iconUrl[0]&@CRLF&$iconJournal[0]&@CRLF&$iconfree[0]&@CRLF&$iconFor[0]

;If Not @error Then MsgBox(0, '', $iconAll)
;Local $iconFor=StringRegExp($html,'(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>', 3)
;_ArrayDisplay($iconFor)

;(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>



 楼主| 发表于 2014-7-18 21:28:11 | 显示全部楼层
又加了一个flag,呵呵。

#cs ----------------------------------------------------------------------------
        
        AutoIt Version: 3.3.12.0
        Author:         myName
        
        Script Function:
        Template AutoIt script.
        
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include<array.au3>
$iconflag=True
Local $html = BinaryToString(InetRead("http://www.ncbi.nlm.nih.gov/pubmed/24406157", 1), 1)
;$html=StringReplace($html,@CRLF,"")

Local $iconUrl = StringRegExp($html, '<a href="([^"]+).+?Icon for', 3)
If Not @error Then 
MsgBox(0, '', $iconUrl[0])
Else
        $iconflag=False
EndIf

Local $iconJournal = StringRegExp($html, 'journal="([^"]+).+?Icon for', 3)
If Not @error Then 
MsgBox(0, '', $iconJournal[0])
Else
        $iconflag=False
EndIf

Local $iconFree = StringRegExp($html, 'free_status="([^"]+).+?Icon for', 3)
#cs
        If @error Then
        $iconFree[0]=False
        MsgBox(0, '', $iconUrl[0])
        Else
        $iconFree[0]=True
        MsgBox(0, '', $iconUrl[0])
        EndIf
#ce
If Not @error Then
        MsgBox(0, '', $iconFree[0])
        $iconFreeflag = True
Else
        $iconFreeflag = False
EndIf
MsgBox(0, '', $iconFreeflag)
;Local $iconFor=StringRegExp($html,'title="([^"]+).+?Icon for', 3)
Local $iconFor = StringRegExp($html, '(?<=Icon for )[\w\h]*', 3)
If Not @error Then 
MsgBox(0, '', $iconFor[0])
Else
        $iconflag=False
EndIf
MsgBox(0,"iconflag",$iconflag)
;MsgBox(0,0,$iconFor[0])
;Local $iconFor2=StringReplace($iconFor[0],"Read full text in ","")
;Local $iconAll=$iconUrl[0]&@CRLF&$iconJournal[0]&@CRLF&$iconfree[0]&@CRLF&$iconFor[0]

;If Not @error Then MsgBox(0, '', $iconAll)
;Local $iconFor=StringRegExp($html,'(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>', 3)
;_ArrayDisplay($iconFor)

;(?i)journal="(.+?)".+?"(.+?)".+?"([^"]+?)">[\s\S]+?([^"]+?)"></img>


您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-8 22:13 , Processed in 0.088068 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表