Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to write a regular expression which will take only last 3 char of a string and append some constant string to it.

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#.

Something like :

stringVal.Trim().Substring(0, stringVal.Trim().Length - 3) + ".ConstantValue"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
197 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use this regular expression :

.{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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...