正则表达式:在两个定界符之间获取第n个字符 [英] Regex: Get nth character between two delimiters

查看:563
本文介绍了正则表达式:在两个定界符之间获取第n个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例字符串

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆