正则表达式:提取最后 2 个字符 [英] regular expression: extract last 2 characters

查看:498
本文介绍了正则表达式:提取最后 2 个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用正则表达式提取字符串最后 2 个字符的最佳方法是什么.

what is the best way to extract last 2 characters of a string using regular expression.

例如,我想从下面提取状态代码

For example, I want to extract state code from the following

A_IL"

我想将 IL 提取为字符串..

I want to extract IL as string..

请向我提供有关如何获取它的 C# 代码..

please provide me C# code on how to get it..

string fullexpression = "A_IL";
string StateCode = some regular expression code....

谢谢

推荐答案

使用正则表达式:

 ..$

这将返回提供结束锚点旁边的两个字符.

This will return provide the two characters next to the end anchor.

由于您使用的是 C#,这会更简单,而且可能更快:

Since you're using C#, this would be simpler and probably faster:

string fullexpression = "A_IL";
string StateCode = fullexpression.Substring(fullexpression.Length - 2);

这篇关于正则表达式:提取最后 2 个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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