TCL 正则表达式 [英] TCL Regular Expression

查看:50
本文介绍了TCL 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种类型的接口Ethernet9/12/1 和 Ethernet8/34".我想使用单个正则表达式解析这两种类型的接口.

I have two types of interfaces "Ethernet9/12/1 and Ethernet8/34". I wanted to parse both type of interfaces using single regular expression.

我试图通过以下方式实现这一目标:

I was trying to achieve this in the below manner:

set int "Ethernet9/12/1"
regexp -nocase "(.*t)((\\d+)\/(\\d+)/\(\\d+))" $int - int_type int_num mod_num port broken_port
puts $int_type
puts $int_num
puts $mod_num
puts $port
puts $broken_port

此代码适用于 Ethernet9/12/1.但是,相同的正则表达式不适用于 Ethernet8/34.我需要帮助才能使用 TCL 在单个正则表达式中实现这一点.

This code works for Ethernet9/12/1. But, the same regexp does not work for Ethernet8/34. I need help to achieve this in the single regexp using TCL.

请帮忙.

推荐答案

您需要使用非捕获组将最后一部分设为可选:

You need to make the last part optional with a non-capturing group:

regexp -nocase "(.*t)((\\d+)\/(\\d+)(?:/(\\d+))?)" $int - int_type int_num mod_num port broken_port
                                    ^^^       ^^

参见 IDEONE Tcl 演示

输出:

Ethernet
8/34
8
34

非捕获组仅用于对子模式进行分组,并且不会干扰您已定义以供以后使用的捕获组.

A non-capturing group is only meant to group subpatterns and does not interfere with the capturing groups you already defined to be used later.

这篇关于TCL 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆