[已解决]查询字符串中特定字符所对应的值
本帖最后由 fuinei 于 2012-3-9 09:28 编辑大家好,目前需修改一个小程序遇到关于字符串的问题,请问在字符串中如何查询某个字符串所对应的值呢?
如字符串内容为:
The score as following:
Peter = 60
Ken = 65
如何查询Peter的成绩呢?
注:
姓名所对应(等号后面)的值会发生变化 Local $Str = _
'The score as following:' & @CRLF & _
'Peter = 60' & @CRLF & _
'Ken = 65'
Local $n = 'Peter'
Local $Test = StringRegExp($str, '\n' & $n & '\h*=\h*(\V+)\h*(?=\v|$)', 3)
If Not @Error Then MsgBox(0, $n, $Test) 谢谢版主的快速回复,如程序输出的在同一行上如“The score as following:Perter =60,Ken=65,Ann=70”
结果只希望输入Perte成绩60,请问代码可以如何修改呢? 回复 3# fuinei Local $Str = 'The score as following:Perter =60,Ken=65,Ann=70'
Local $n = 'Perter'
Local $Test = StringRegExp($str, '(?::|,)\h*' & $n & '\h*=(.+?)\h*(?=,|$)', 1)
If Not @Error Then MsgBox(0, $n, $Test)
Local $n = 'Ken'
Local $Test = StringRegExp($str, '(?::|,)\h*' & $n & '\h*=(.+?)\h*(?=,|$)', 1)
If Not @Error Then MsgBox(0, $n, $Test) 本帖最后由 afan 于 2012-3-2 19:40 编辑
所有的:#include <Array.au3>
Local $Str = 'The score as following:Perter =60,Ken=65,Ann=70'
Local $Test = StringRegExp($str, '[:,]\h*(.+?)\h*=(.+?)\h*(?=,|$)', 3)
_ArrayDisplay($Test, UBound($Test)) 谢谢版主的指导,从上面几个例出了解到重点还是在于正则表达式.
刚入门不久,正达表达式使用起来感觉比较难,今天下午调试了近1个小时仍获取不到需要的值.
我通过TCPSend发送数据(show cellular 0/0/0 radio)后通过TCPRecv可获取以下数据
1921#sh cellular 0/0/0 radio
Current Band = WCDMA 2100, Channel Number = 10663
Current RSSI = -64 dBm
Band Selected = WCDMA I IMT 2000 (EU)
Number of nearby cells = 1
Cell 1
Primary Scrambling Code = 0x1F9
RSCP = -68 dBm, ECIO = -3 dBm
1921#
我的目标是获取Current RSSI所对应的值-64 dBm,希望版主能再指导一下怎样的正则表达式可返回值-64 dBm,谢谢! $Test = StringRegExp($str, 'RSSI\h*=\h*(\V+)', 1) 本帖最后由 shqf 于 2012-3-4 08:35 编辑
Local $Str = _
"Current Band = WCDMA 2100, Channel Number = 10663" & @CRLF & _
"Current RSSI = -64 dBm" & @CRLF & _
"Band Selected = WCDMA I IMT 2000 (EU)"
Local $test = StringRegExp($Str, 'RSSI = (.+dBm)', 1)
ConsoleWrite($test) 谢谢afan版主和shqf兄的指导,两位提供的正则表达式使用字符串测试时都可返回所需的值-XX dBm.
但从TCPRecv中查询RSSI所对应值时却返回空值,我的代码如下:
TCPStartup()
$socket = TCPConnect("192.168.1.1",23)
Sleep(1000)
TCPSend($socket, "admin" & @CRLF)
Sleep(1000)
TCPSend($socket, "123456" & @CRLF)
Sleep(1000)
$ack = TCPRecv($socket, 500)
;MsgBox(0,"info",$ack)
TCPSend($socket, "show cellular 0/0/0 radio" & @CRLF)
Sleep(1000)
$ack = TCPRecv($socket, 600)
MsgBox(0,"info",$ack)
Sleep(1000)
$Signal = StringRegExp($ack, 'RSSI = (.+dBm)', 1)
MsgBox(0,"Current Signal is",$Signal)
TCPCloseSocket($socket)
TCPShutdown()
Exit
代码在运行中会弹出一个包含RSSI值的窗口,信息如下:
sh cellular 0/0/0 radio
Current Band = WCDMA 2100, Channel Number = 10663
Current RSSI = -64 dBm
Band Selected = WCDMA I IMT 2000 (EU)
Number of nearby cells = 1
Cell 1
Primary Scrambling Code = 0x1F9
RSCP = -68 dBm, ECIO = -3 dBm
1921#
请问两位如何修改可使用其返回RSSI所对应的值呢? 本帖最后由 shqf 于 2012-3-4 10:51 编辑
是个小问题,不劳afan大驾,我来回答吧。StringRegExp($ack, 'RSSI = (.+dBm)', 1)返回值会存入到数组中,所以$Signal是个数组变量。故 MsgBox(0,"Current Signal is",$Signal)一句中应用$Signal,你可再一试。 谢谢shqf兄的指导,测试已成功,明天加到原来的代码中再调试一下,非常感谢! 修改了原来的代码需实现的功能已经OK,但有一点美中不足,从字符串中返回的值在值与单位之间存在空格字格,如(-64 dBm),请问shqf兄从字符串中只需返回具体的信号强度(不包括单位)的正则表达示如何编写呢?
如Current RSSI = -64 dBm只返回-64(后面不包空格的),我按照你所提供的表达式稍修改了一下返回值总包括空格 请指导如需在下面的字符串中返回值(-64)的正则表达式,谢谢!
sh cellular 0/0/0 radio
Current Band = WCDMA 2100, Channel Number = 10663
Current RSSI = -64 dBm
Band Selected = WCDMA I IMT 2000 (EU)
Number of nearby cells = 1
Cell 1
Primary Scrambling Code = 0x1F9
RSCP = -68 dBm, ECIO = -3 dBm
1921# RSSI = (.+\d\d) 本帖最后由 shqf 于 2012-3-5 20:04 编辑
RSSI = (.+) dBm 意为:匹配的字串其左边为“RSSI = ”,其右边为“ dBm”,括号表示获取中间部分。
页:
[1]
2