2016-11-13 29 views
0

需要從這個巨大的字符串(1618252)中獲取作業ID並且沒有正則表達式來執行此操作,因爲它可能會失敗時作業ID是沒有找到----------tcl需要一種方法來從巨大的字符串中獲取作業ID無正則表達式使用作爲案例可能會失敗

% set invoc [[$this cget -testrun] cget -invocation] 
tcl profilemgr -configNetwork {} -startBefore now -releaseUnusedEquip 0 \ 
    -profile ptse1-bsd2 -noreserve 0 -noreplace 0 \ 
    -comment {<a  href="/testrunscheduler/schedulerJob.rvt?job_id=1618252">Profile Push:  1618252</a>} \ 
    -attr {} -dur 300minutes -controlAssert 0 -roleSubst {} -offline false -nodb 0 \ 
    -image {} -layer2Mode mapping -checkLeaks 0 -config {} -trace 0 -debug 0 -desc 0 \ 
    -notify 0 -forceProfileIP false -clusterHashingMode simple -doConfig true \ 
    -reservation 0 -projects pts_7_20-latest -flavor {} -platformMix {} \ 
    -releaseReservation 0 -pkgExt {} -emails {} -loadBalancingMode none -enableIOM false \ 
    -checkConn 1 -platform ptsvpl-esxi-lnx -pkgView {} -offlineFailedConn 1 -noall 0 \ 
    -ipMode ipv4 -runType pmgr-run -enableUdpPrioritization false \ 
    -params {profilemgr:profileToPush "ptse1-bsd2" profilemgr:platform "ptsvpl-esxi-lnx"} \ 
    -swtc_view /m/vmurshilly_lab \ 
    {<br>Invocation with all defaults removed => tcl profilemgr -profile "ptse1-bsd2" -comment "<a href="/testrunscheduler/schedulerJob.rvt?job_id=1618252">Profile Push: 1618252</a>" -dur "300minutes" -projects "pts_7_20-latest" -platform "ptsvpl-esxi-lnx" -offlineFailedConn "1"} 

我嘗試使用正則表達式本身,這失敗:

~$tcl 
% regexp -all .*job_id=(.*)\"> "supercalafrajilistic" match jobID 
0 
% puts $jobID 
can't read "jobID": no such variable 
while evaluating {puts $jobID} 
+2

通常的習慣用法是在if命令的條件表達式中進行匹配。當條件爲真時,變量jobID存在;當錯誤時,它不會。 (從regexp命令中得到的0表示匹配失敗。) –

+0

我已經在該字符串中添加了一些backslash-newlines。 _他們不是真的在你的數據中,_但他們至少讓人們可以閱讀它! –

回答

0

好了,你沒有運行對字符串的正則表達式,你牽強而只是虛擬的「supercalafrajilistic」數據;這很重要。您還需要以非貪婪模式運行正則表達式,但通過這樣做,我們可以將事情簡化一些。最後,你應該檢查regexp的結果;在你使用的模式下,它返回正則表達式匹配的次數,如果發現了某些事情,這實際上是一個布爾測試。

set invoc [[$this cget -testrun] cget -invocation] 
if {[regexp -all {job_id=(.*?)\"} $invoc -> jobID]} { 
    puts "the job ID is $jobID; yippee" 
} else { 
    # Up to you how to recover from this failure case... 
    puts "warning: no job ID found" 
} 

除此之外,它與RE的Tcl中常見的東西(把你的正則表達式中括號,除非你知道你不應該使用->而不是match,如果你不關心總匹配字符串以提高可讀性等)

+0

但問題是我沒有得到同樣的作業ID每次爲不同的字符串: 我得到的結果如下: %投入$ JOBID 1619243&項目= vin_trs_debug&test_group = new_test_group_vin與平臺= ptsvpl-的ESXi-LNX 需要正則表達式,這將取從任何巨大的字符串作業ID,正則表達式應限制爲「job_id = 1654533」,並從整個巨大的字符串中提取字符串1654533 @Donal Fellows – vinay

相關問題