Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

c# - Remove decimal point when not between two digits

I'm cleaning up a search string and need to remove any periods that appear but keep decimal points when they are between two digits.

For example if I have a string

599.75, Tigers.

Then I would like it to come back as

599.75, Tigers

I was thinking a line like:

strNewString = RegEx.Replace(strOrigString, strRegEx, string.Empty);

Where strRegEx would match just the .'s to remove but I'm having a hard time figuring out how to match only the . and not the things surrounding it as well.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You should take advantage of the lookahead and lookbehind assertions. They don't actually match characters in your input, but only determine whether a match is possible or not.
You can use negative lookaheads and negative lookbehinds to do the opposite of this, which is what's appropriate here. Using the following for strRegEx will match periods that are not surrounded by digits:

(?<!d).(?!d)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...