![](/img/trans.png)
[英]How to remove extra dots spaces from a string to build a decimal using C# Regex?
[英]Regex to remove dots from string
我有這個
regex Regex.Replace(listing.Company, @"[^A-Za-z0-9_\.~]+", "-");
listing.Company是一個字符串,可以使用,但是當字符串中包含點時,不會刪除它們。
你能幫我一下嗎
在當前的正則表達式中,您有\\.
在您的排除中,這將導致Regex.Replace
忽略Regex.Replace
。 另外,您的正則表達式不執行任何操作以將輸入字符串轉換為小寫。 您可以在之后執行此操作,但是在替換之前執行此操作可使您的模式更簡單。
試試這個方法:
var output = Regex.Replace(listing.Company.ToLower(), "[^a-z0-9_]+", "-");
嘗試
Regex.Replace(listing.Company.ToLower(), @"[^a-z0-9_]+", "-");
您不包括\\.
這是點。 另外,如果要使用小寫字母,則需要先將字符串轉換為小寫。
也許您正在尋找這樣的東西:
string res = Regex.Replace(listing.Company, @"[\W+\.~]", "-");
在這里,正則表達式引擎將查找除AZ, az, underscore
以及dot
和~
以外的任何字符,並將其替換為“-”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.