正则表达式:在两个定界符之间获取第n个字符 [英] Regex: Get nth character between two delimiters
问题描述
我有一个示例字符串
1836248_NNY_01.pdf
但也可以
18362481_YYN_102.pdf
我需要在两个下划线之间得到第n个字符.
到目前为止,我的正则表达式是\_(.*?)\_
,以便在下划线之间得到字符.但是跟进之后,我不知道如何获得第二个N.
https://regex101.com/r/XUMKyf/1/ >
您可以使用\_.{1}(.).*\_
并将1
替换为所需的任何内容.
因此,0
将是第一个字符,1
是第二个字符,依此类推.
示例: https://regex101.com/r/XUMKyf/3 >
I have an example string
1836248_NNY_01.pdf
but it can also be
18362481_YYN_102.pdf
And I need to get the nth character between the two underscores.
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.
https://regex101.com/r/XUMKyf/1/
You could use \_.{1}(.).*\_
and replace the 1
with whatever you want.
So 0
would be the first char, 1
the second, and so on.
Example: https://regex101.com/r/XUMKyf/3
这篇关于正则表达式:在两个定界符之间获取第n个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!