简体   繁体   English

tcl regexp根据字符串的索引匹配数字字符串

[英]tcl regexp match a string of digits based on index of the string

I am in need of a little help. 我需要一点帮助。 I am trying to come up with a regexp to match a string of digits. 我正在尝试提出一个正则表达式来匹配数字字符串。 I want the string to match starting at a position value of the string I have. 我希望字符串从我拥有的字符串的位置值开始匹配。

here is the string I have 这是我的字符串

01102000000000001A40000000200000000000100012064000100000000000000

I want the regexp to match starting from the 20th value of the string or directly after the A4. 我希望正则表达式匹配从字符串的第20个值开始或直接在A4之后。 Also I want to match the first say 10 digits after the 20th value of the string. 我也想匹配字符串第20个值后的第一个说10位数。 I have tried a few things they are not really working. 我已经尝试了一些没有用的东西。 Wanted to see if I could get some idea's here. 想看看我是否可以在这里得到一些想法。

In case the link in TessellatingHeckler's answer goes stale, I'll add another answer with the actual code. 如果TessellatingHeckler的答案中的链接过时,我将在实际代码中添加另一个答案。 There are at least three solutions to this problem (assume the string to search in is in the variable s ): 该问题至少有三种解决方案(假设要搜索的字符串在变量s ):

TessellatingHeckler's solution: TessellatingHeckler的解决方案:

lindex [regexp -inline {.{19}(\d{10})} $s] 1

Another regexp solution: 另一个正则regexp解决方案:

regexp -inline -start 19 {\d{10}} $s

A string range solution: string range解决方案:

string range $s 19 19+9

Documentation: lindex , regexp , string 文档: lindexregexp字符串

A regex to match the first 19 characters up to A4 and throw them away, then capture 10 digits after that into a group: 一个正则表达式,用于匹配前19个字符(最多A4)并将其丢弃,然后将其后的10个数字捕获为一组:

.{19}(\d{10})

http://regex101.com/r/cO9bE7/1 http://regex101.com/r/cO9bE7/1

(I don't know if it's TCL compatible, though). (不过,我不知道它是否与TCL兼容)。

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

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