简体   繁体   中英

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.

The 4 basic string types are

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. With the strings above I want to capture AVDD, DVDD_15, AGND, and 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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