正则表达式获取字符串的后3个字符 [英] Regular expression to get last 3 characters of a string
本文介绍了正则表达式获取字符串的后3个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想编写一个仅包含字符串最后3个字符并向其附加一些常量字符串的正则表达式.
I want to write a regular expression which will take only last 3 char of a string and append some constant string to it.
我正在使用C#.我正在尝试将正则表达式作为数据库条目.稍后在应用程序中阅读此条目,并在C#中基于正则表达式进行转换.
I am using C#. I am trying to make regular expression as database entry. Later Read this entry in application and do the transformation based on regex in C#.
类似的东西:
stringVal.Trim().Substring(0, stringVal.Trim().Length - 3) + ".ConstantValue"
推荐答案
使用此正则表达式:
.{3}$
如果您想避免在末尾留空格并可以使用捕获组(您没有精确说明语言或正则表达式的味道),请使用
If you want to avoid spaces at end and can use capturing groups (you didn't precise the language or regex flavour), use
(.{3})\s*$
但是请注意,没有明显的理由在这里使用正则表达式而不是对字符串进行切片.
But note that there's no obvious reason to use a regex here instead of slicing the string.
这篇关于正则表达式获取字符串的后3个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文