简体   繁体   English

正则表达式:在两个定界符之间获取第n个字符

[英]Regex: Get nth character between two delimiters

I have an example string 我有一个示例字符串

1836248_NNY_01.pdf 1836248_NNY_01.pdf

but it can also be 但也可以是

18362481_YYN_102.pdf 18362481_YYN_102.pdf

And I need to get the nth character between the two underscores. 我需要在两个下划线之间得到第n个字符。

So far my regex is \\_(.*?)\\_ to get the characters between the underscores. 到目前为止,我的正则表达式是\\_(.*?)\\_以获取下划线之间的字符。 But following up I can't figure out how to get the 2nd N for example. 但是跟进之后,我无法弄清楚如何获得第二个N。

https://regex101.com/r/XUMKyf/1/ https://regex101.com/r/XUMKyf/1/

You could use \\_.{1}(.).*\\_ and replace the 1 with whatever you want. 您可以使用\\_.{1}(.).*\\_并将1替换为所需的内容。
So 0 would be the first char, 1 the second, and so on. 因此, 0是第一个字符, 1是第二个字符,依此类推。

Example: https://regex101.com/r/XUMKyf/3 范例: https//regex101.com/r/XUMKyf/3

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

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