sex123 发表于 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="Public Library of Science"
$Linkout="http://dx.plos.org/10.1371/journal.pone.0094898"
$Linkout="Europe PubMed Central"
$Linkout="http://europepmc.org/abstract/MED/24755572"
.....
$Linkout=....
$Linkout=...
谢谢。

hnfeng 发表于 2014-7-16 09:23:04

好像你这个不需要正则就可以处理,直接用 StringInStr 试一试

shqf 发表于 2014-7-16 12:59:45

本帖最后由 shqf 于 2014-7-16 13:03 编辑

既然是想用正则解决,就只要告诉源字符串内容及想要获取的字符串内容就行了。

sex123 发表于 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>

sex123 发表于 2014-7-18 12:08:53

顶起, 为什么没人帮助呢?

shqf 发表于 2014-7-18 13:04:50

整个这段内容要提取出来,还是这一段中的某些内容要提取? 如果是后者,那么具体提取什么内容、哪些内容能说清楚吗?
不说清楚,没人能帮你,没法帮你呵。

haijie1223 发表于 2014-7-18 13:11:11

正则在你的帖子中不占主导地位~

sex123 发表于 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
要通过正则取得这四个关键的变化内容

afan 发表于 2014-7-18 14:54:25

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

shqf 发表于 2014-7-18 15:18:03

本帖最后由 shqf 于 2014-7-18 15:19 编辑

A大的正则总那么精简,学习了。我的代码长了点,献丑了,仅供楼主参考:(?i)(?s)journal="(.+?)" free_status="(.+?)".+href="(.+?)".+alt="(.+?)"></img>

sex123 发表于 2014-7-18 17:29:55


afan 发表于 2014-7-18 14:54 http://www.autoitx.com/images/common/back.gif


   

#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=StringRegExp($html,'Icon for.+?', 3)
;MsgBox(0,0,$iconFor)
;Local $iconFor2=StringReplace($iconFor,"Read full text in ","")
;Local $iconAll=$iconUrl&@CRLF&$iconJournal&@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>


为什么没有反应就退出了呢?

afan 发表于 2014-7-18 17:42:15

回复 11# sex123


    根本没有你贴出的示例字符串

sex123 发表于 2014-7-18 18:46:01

回复sex123


    根本没有你贴出的示例字符串
afan 发表于 2014-7-18 17:42 http://www.autoitx.com/images/common/back.gif


    不会吧,网址也对呀,是什么原因造成的呢?

sex123 发表于 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)
Local $iconJournal = StringRegExp($html, 'journal="([^"]+).+?Icon for', 3)
If Not @error Then MsgBox(0, '', $iconJournal)
Local $iconFree = StringRegExp($html, 'free_status="([^"]+).+?Icon for', 3)
#cs
        If @error Then
        $iconFree=False
        MsgBox(0, '', $iconUrl)
        Else
        $iconFree=True
        MsgBox(0, '', $iconUrl)
        EndIf
#ce
If Not @error Then
        MsgBox(0, '', $iconFree)
        $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)
;MsgBox(0,0,$iconFor)
;Local $iconFor2=StringReplace($iconFor,"Read full text in ","")
;Local $iconAll=$iconUrl&@CRLF&$iconJournal&@CRLF&$iconfree&@CRLF&$iconFor

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

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



sex123 发表于 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)
Else
        $iconflag=False
EndIf

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

Local $iconFree = StringRegExp($html, 'free_status="([^"]+).+?Icon for', 3)
#cs
        If @error Then
        $iconFree=False
        MsgBox(0, '', $iconUrl)
        Else
        $iconFree=True
        MsgBox(0, '', $iconUrl)
        EndIf
#ce
If Not @error Then
        MsgBox(0, '', $iconFree)
        $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)
Else
        $iconflag=False
EndIf
MsgBox(0,"iconflag",$iconflag)
;MsgBox(0,0,$iconFor)
;Local $iconFor2=StringReplace($iconFor,"Read full text in ","")
;Local $iconAll=$iconUrl&@CRLF&$iconJournal&@CRLF&$iconfree&@CRLF&$iconFor

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

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


页: [1] 2
查看完整版本: 求正则一例