简体   繁体   English

tcl正则表达式,尝试在两个模式之间提取一个字符串

[英]tcl regular expression, attempting to pull out a string between two patterns

Gretings! 问候! I am trying to use tcl regular expressions to strip off unwanted characters and keep the desired string. 我正在尝试使用tcl正则表达式去除不必要的字符并保留所需的字符串。

The 4 basic string types are 4种基本字符串类型是

I34/pAVDD_3
I32/pDVDD_15_2
I999/pAGND
I3/pDOUT_LG0

What I want to capture is what's in-between the p and the end of the string or the last underscore & number if it exists. 我要捕获的是p和字符串的末尾之间(如果存在的话,最后一个下划线和数字之间)。 With the strings above I want to capture AVDD, DVDD_15, AGND, and DOUT_LG0. 使用上面的字符串,我想捕获AVDD,DVDD_15,AGND和DOUT_LG0。

I thought I had it with [p](\\w*)?[_][\\d*] but it doesn't work with I3/pDOUT_LG0 and after quite awhile of trying different things, I can't find a pattern that will work. 我以为我用[p](\\w*)?[_][\\d*]它,但它不适用于I3 / pDOUT_LG0,经过一段时间尝试了不同的尝试之后,我无法找到一种模式将工作。

Thanks! 谢谢!

How about 怎么样

regexp {p(?:(\w+)_\d|(\w+))$} $str -> c1 c2
set result $c1$c2

One or the other will be empty, so the result is a simple concatenation of them. 一个或另一个将为空,因此结果是它们的简单连接。

Another possible solution is to strip off the unwanted parts: 另一种可能的解决方案是剥离不需要的部分:

regsub -all {.+p|_\d$} $str {}

Documentation: regexp , regsub , Syntax of Tcl regular expressions 文档: regexpregsubTcl正则表达式的语法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM