Regex: Get nth character between two delimiters

ぐ巨炮叔叔 提交于 2019-12-11 15:17:44

问题


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/


回答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



来源:https://stackoverflow.com/questions/53636059/regex-get-nth-character-between-two-delimiters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!