2015-11-12 149 views
0

我想知道是否可以創建一個允許數字,字母和空格但沒有標點符號的正則表達式。不允許輸入標點符號

發生了什麼是我有一個在線貸款申請和街道地址字段中的一些用戶在他們的輸入中添加逗號(,)。在將應用程序導出爲CSV時,由於地址字段中包含逗號,因此CSV文件在錯誤的地方包含逗號。

我一直在玩一個正則表達式幾個小時,現在很好,但它不能完美。我偶爾會得到一個無法驗證的地址,但它沒有標點符號。否則它會是一個可接受的輸入,但是正則表達式不允許它被驗證。

所以不是我想什麼規定是允許(數字,空格,字母),我不能只是規定了什麼是允許(標點符號)?後者比前者更有意義。

爲了提供信息,我已經包含了用於測試我的正則表達式的地址列表。我還包括我一直在努力的正則表達式。任何指導或協助將不勝感激

注:我已添加註釋,其中輸入無效。在無效輸入下面,我放置了一個現在有效的輸入的編輯副本。

地址:

3 TINDERWOOD CRESCENT 



117HEIDERAND FLATS EESSENTHOUT STREET HEIDERAND 6511 -- no space after number 

117 HEIDERAND FLATS EESSENTHOUT STREET HEIDERAND 6511 



3289 ext5 edenpark 

120411 mpande loation 

1433b Moulton Avenue 

1433 b Moulton Avenue 

4 diesel rd 

10821 Morubisi Street 



Unit 44 Charis Place, Prosperity Road, Groblerspark -- punctuation 

Unit 44 Charis Place Prosperity Road Groblerspark -- starts with word 

44 Charis Place Prosperity Road Groblerspark 



p box 3581  -- invalid street address 



82 Akasia laan 

987 leruleng sectionsaulspoort 

1 lenton drive 

1179 gugulethu street 

1 lenton drive 



10269verdwaal2 -- no spaces 

10269 verdwaal 2 



15 Prinsloo Street 

1179 gugulethu street 



13 Adler str, Eden Park -- has punctuation 

13 Adler str Eden Park 



410 wonderzicht, 538 de beerstr -- has punctuation 

410 wonderzicht 538 de beerstr 



1551  -- invalid street address 



52 Koedoe Ave 

26a high road orchards 



Musina ext9 4363 -- number in wrong place - See below 

4363 Musina ext9 



18 Replubliek street 

753 steve biko avenue unit2 

221 Buitekant straat 

54 Zone A 

B1287 UMSOLWA ROAD 

54 Zone A 

6574 LETLHODI STREET UNIT 14 

27 k street 

9635 Zanemvula street 

2667 nthatisi Street 

498 CESSNA AVENUE 



maromene 17   -- number in incorrect place - see below 

17 maromene 



F1 Ngoje cres MarainnhillM 

6574 letlhodi street unit 14 

21 tecomastreet 

21 tecomastreet 

07 Frara Drive 

6440 MAYANA STREET THEMBALETHU GEORGE 6529 



no.87 Vista Villas  -- has word before number and has punctuation 

87 Vista Villas 



12 jama street z section --- !!!!!!!!!!!!!!!!!!!!!!!! 



B1287 Umsolwa road 

B 1287 Umsolwa road  -- space after B 



6574 letlhodi street 

7658 Itsomo Steet Ext6 



5dharrisson str  -- no space fter number 

5d harrisson str 



322 Lenham Drive, Lenham -- has punctuation 

322 Lenham Drive Lenham 



schoolstreet 3  -- name before number 

3 schoolstreet 



50 Hercules Court 

1546 sefatsa stand 

61 sixwila street 

20800 mamelodi east 



colchester crescent 



12 Iraq Street 

12 moshoeshoe str 

21 vansoelen street 

12 moshoeshoe str 

4102 Geelhout Street 



ward 16, umzumbe, hibberdene  -- has punctuation 

ward 16 umzumbe hibberdene 



839 Maokeng ext 



28caledon drive 



34 Leo ave 



6423 Bandura Street, Willow Manor Ext. 1 



8541 Snake Park 

7 INSISWA WAY EXT 12 SHERWOOD PARK 7349 



plot 50 kareebos 



404 Windermere street 

404 Windermere street 

404 Windermere street 

68 Flamink Street 

68 Flamink Street 

71 Eagle Dawn Zeiss road 



block r3 room 105 n2 gateway 



50 plein str 

B206 Chapters 

225 Buitenkant Street 

9 Chestnut street Bonteheuwel 

1740a Ben Naude Drive Zone 2 



Droedam Farm 



16 Aberfeldy Road The Hill 



11th road 25 simonsig noordwyk 



49 Soetdoring 



Allensnek 


12935 


29 likhonda street 

6 Matume street 



Plot 88 



22 Community Road 


1100 kwamakhutha township, po amanzimtoti 



9 balmoral heights, balmoral road 



3 makubalo streets 

3951 umthungwa street birch acres ext 23 

12986 Walter Sisulu Street 

11138 nongoma str 

51 rudloff rd 



10 Villa Palazzo, Belami Drive 



321 Francis Baard Street 

65 9 th street 

35 Fiskaal street 

14 olive street 

9 manho street 

38 victoria road 

354 Ethafeni Section 

16 Arkeldien Street 



6.5 Boundary Road 



100 Vlamboom Road 

902 Mquma Street Bophelong 

35 amsterdam street 

55 fanplam gardens palmview 

6213 Zone 12 Extion Sebokeng 



stand no.043 ooslope 



3 MURISON STREET 

177 govan mbeki township 

9 Chestnut street Bonteheuwel 

4347 BLOCK B 

1a roy cambell street 

146 poole avenue 

116 Woburn Street 

545 Phase 5B Buhlepark 

33 vos street 



37 fifth street , rusthof 



18 Marinda Crescent Marinda 

16 chromite ave 



13 kanti street 35263,Harare 



67 Third Street 

270 queen elizabeth ave 

30 Victoria 

9 manho str 

26 Liebenhof Flats Young Str 

1 Carter Street 



10 Villa Palazzo, Belami Drive 



11 vlasblomsingel progress 



block 44, 513 sunset avenue 



1073 mphafa road 

1344 Pickerel place 

1073 mphafa road 



sweatpealaan 2751 



44 Brummerstreet 

151 Ext 3 

433 10 kodi street 

617 rondomstraat Louterwater 

10 GREEN STREET 

149 tamsanqa street 

3 Newfeld Street 

76 Dorp Street 

5 Mabille Street 

28 heathbury place 

2356 joyce ndinisa road 

A13 Lekoane street 

正則表達式:

/^(\w*)(\d+)(\w)?\s(\w+(\s?\w+?))+$/ 
+0

你的意思是像這樣的,從中可以添加一個空格,刪除下劃線? http://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-underscores或任何數量的其他例子,你可以搜索「字母數字正則表達式」 – Marc

+4

以前問:http://stackoverflow.com/questions/4328500/how-can-i-strip-all-by-a-string-in-javascript-using-regex –

+0

如果你採用MarcosPérezGude建議的路線,你可以讓用戶輸入幾乎任何東西......可能會感興趣。 – LGSon

回答

1

爲什麼你需要爲這個正則表達式?你可以這樣簡單的東西:

var address = "My address, with commas not allowed"; 
var newAddress = address.replace(",",""); 

正如我在評論說,用這種方法,你允許你的用戶寫的地址,因爲他們想要的,但不破壞您的CSV分隔符。您將能夠寫一個地址,這樣

街無處Nº10 - 2(12000)

而且它不會破壞你的CSV。正則表達式,該地址將是這樣的:

街無處N 10 2 12000

這是較少的可讀性。

+0

我猜_...允許數字,字母和空格但沒有標點符號的正則表達式會使這種方法變得困難:) – LGSon

+0

OP的問題是CSV讀取的逗號分隔符。我提供了一種簡單的方法,允許用戶在不中斷CSV分隔的情況下編寫他們所需的內容。但是,如果你感覺更好,當你冷靜下來時,歡迎你的降價。 –

+0

我沒有投票,我反而評論。我認爲你可以更清楚地說明你爲什麼建議你的方法(如你在評論中),這可能會使你免於進一步倒票。 – LGSon

1

更好的解決方案是用逗號逃離現場。見https://stackoverflow.com/a/769675/227299。您還需要轉義任何雙引號。

如果您在JavaScript這樣做,你可以這樣做:

function escapeCsvValue(val) { 
 
    if (val.indexOf(',') > -1 || val.indexOf('"') > -1) { 
 
     return '"' + val.replace(/"/g, '""') + '"'; 
 
    } 
 
    return val; 
 
} 
 
var csv = [ 
 
    ['Name, CommaForFun', 'Address'], 
 
    ['Jack','Calle Ocidental, 2'], 
 
    ['Jill O"quote"','125, The "cool" place'], 
 
    ['John','123 Main st'], 
 
]; 
 
    
 
var escapedCsv = csv.map(function(row){ 
 
    return row.map(escapeCsvValue).join(','); 
 
}); 
 

 
var csvString = escapedCsv.join('\n'); 
 
console.log(csvString); 
 
/* 
 
"Name, CommaForFun",Address 
 
Jack,"Calle Ocidental, 2" 
 
"Jill O""quote""","125, The ""cool"" place" 
 
John,123 Main st 
 
*/