⑴ 如何用正则表达式只提取字符串中位于后面的连续数字
我来提供个思路:
1、先通过替换功能,将正则表达式(^>.*<name>)\r\n替换为\1,将数字和<name>放到同一行
2、再用正则表达式提取出数字
⑵ 正则表达式提取数字
^=\d{10}=$
⑶ 求一个或两个正则表达式,提取这段代码里的数字
<?php
$str = <<<EOT
GigabitEthernet2/3 is up, line protocol is up (connected)
Hardware is 1000Mb 802.3, address is 001f.6d1b.e400 (bia 001f.6d1b.e400)
Description: NEW_to_NE20
Internet address is 192.168.1.1/12
MTU 1500 bytes, BW 1000000 Kbit, DLY 10 usec,
reliability 255/255, txload 7/255, rxload 5/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-plex, 1000Mb/s, media type is SX
input flow-control is off, output flow-control is on
Clock mode is auto
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:00:01, output 00:00:01, output hang never
Last clearing of "show interface" counters never
Input queue: 0/75/168/168 (size/max/drops/flushes); Total output drops: 66
Queueing strategy: fifo
Output queue: 0/40 (size/max)
5 minute input rate 19802000 bits/sec, 5791 packets/sec
5 minute output rate 30458000 bits/sec, 5972 packets/sec
L2 Switched: ucast: 1853118 pkt, 177980905 bytes - mcast: 57932 pkt, 3707648 bytes
L3 in Switched: ucast: 56306228298 pkt, 20771285750237 bytes - mcast: 0 pkt, 0 bytes mcast
L3 out Switched: ucast: 70473614094 pkt, 72715904553757 bytes mcast: 0 pkt, 0 bytes
56311433744 packets input, 20772032140156 bytes, 0 no buffer
Received 57932 broadcasts (0 IP multicasts)
1 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog, 0 multicast, 0 pause input
0 input packets with dribble condition detected
70478943026 packets output, 72716442212721 bytes, 0 underruns
0 output errors, 0 collisions, 1 interface resets
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier, 0 PAUSE output
0 output buffer failures, 0 output buffers swapped out
EOT;
preg_match_all("/(\d+)\s+bits\/sec|(\d{1})\s+input\s+errors|(\d{1})\s+output\s+errors/",$str,$arr);
print_r( $arr );
?>
测试通过~O(∩_∩)O~
⑷ 在SQL语句中如何用正则取出一个字符串的前几位数字
SQL 取字符串的前几位数字,SQL 关键字 substring
substring 使用方法,参考下列SQL:
declare @T nvarchar(10)
set @T='12345abcde'
select substring(@T,1,5)
结果如下:12345
如果是SQL 写正则表达式判断,只能通过存储过程或函数来处理
SQL 如下:
CREATE FUNCTION dbo.find_regular_expression
(
@source varchar(5000), --需要匹配的源字符串
@regexp varchar(1000),--正则表达式
@ignorecase bit = 0--是否区分大小写,默认为false
)
RETURNS bit--返回结果0-false,1-true
AS
BEGIN
--0(成功)或非零数字(失败),是由 OLE 自动化对象返回的 HRESULT 的整数值。
DECLARE @hr integer
--用于保存返回的对象令牌,以便之后对该对象进行操作
DECLARE @objRegExp integer DECLARE @objMatches integer
--保存结果
DECLARE @results bit
/*
创建 OLE 对象实例,只有 sysadmin 固定服务器角色的成员才能执行 sp_OACreate,并确定机器中有VBScript.RegExp类库
*/
EXEC @hr = sp_OACreate 'VBScript.RegExp', @objRegExp OUTPUT
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
/*
以下三个分别是设置新建对象的三个属性。下面是'VBScript.RegExp'中常用的属性举例:
Dim regEx,Match,Matches '建立变量。
Set regEx = New RegExp '建立一般表达式。
regEx.Pattern= patrn '设置模式。
regEx.IgnoreCase = True '设置是否区分大小写。
regEx.Global=True '设置全局可用性。
set Matches=regEx.Execute(string) '重复匹配集合
RegExpTest = regEx.Execute(strng) '执行搜索。
for each match in matches '重复匹配集合
RetStr=RetStr &"Match found at position "
RetStr=RetStr&Match.FirstIndex&".Match Value is '"
RetStr=RetStr&Match.Value&"'."&vbCRLF Next
RegExpTest=RetStr
*/
EXEC @hr = sp_OASetProperty @objRegExp, 'Pattern', @regexp
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
EXEC @hr = sp_OASetProperty @objRegExp, 'Global', false
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
EXEC @hr = sp_OASetProperty @objRegExp, 'IgnoreCase', @ignorecase
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
--调用对象方法
EXEC @hr = sp_OAMethod @objRegExp, 'Test', @results OUTPUT, @source
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
--释放已创建的 OLE 对象
EXEC @hr = sp_OADestroy @objRegExp
IF @hr <> 0 BEGIN
SET @results = 0
RETURN @results
END
RETURN @results
END
⑸ 如何用正则表达式提取一串数字,数字中间有空格,详情见下面
不清楚这串文字之外还有其他什么文字。
如果字符串只有这些字符,完全没有必要用正则吧,
(1)先用替换方法 将空格“ ”替换为空串“”
(2)再将字符串逆序。
程序不同,函数不一样。
例如,Perl中最简单:
$a= reverse qw/2 2 0 5 0 9 0 0 6 0/;
print $a;
直接就得到了去掉空格并且从后往前的 字符串。
⑹ 正则表达式,提取指定数字
批发(d+),括号里捕捉的就是你要的数据
⑺ 请问如何用正则表达式提取出文本中<name>后面的数字(
要用PilotEdit (不是Lite版)
用这个正则表达式来提取:
<name>*$[ ][ ][0-9]+[|!0-9]*$
%05
⑻ 如何用正则表达式提取指定位数内容
原理:匹配需要匹配第N个数字时,(数字+非数字)N-1次+数字,最后的数字即为结果。提取第四个数字(?:\d+[^\d\r\n]+){3}(\d+).*\b{3}的3=4-1提取第二个数字:(?:\d+[^\d\r\n]+){1}(\d+).*\b{1}的1=2-1如果上面数值提取是一行一次匹配