1
1

Extensions.cs 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Threading.Tasks;
  5. using Newtonsoft.Json;
  6. namespace Masuit.Tools
  7. {
  8. /// <summary>
  9. /// 扩展方法
  10. /// </summary>
  11. public static class Extensions
  12. {
  13. #region SyncForEach
  14. /// <summary>
  15. /// 遍历数组
  16. /// </summary>
  17. /// <param name="objs"></param>
  18. /// <param name="action">回调方法</param>
  19. public static void ForEach(this object[] objs, Action<object> action)
  20. {
  21. foreach (var o in objs)
  22. {
  23. action(o);
  24. }
  25. }
  26. /// <summary>
  27. /// 遍历IEnumerable
  28. /// </summary>
  29. /// <param name="objs"></param>
  30. /// <param name="action">回调方法</param>
  31. public static void ForEach(this IEnumerable<dynamic> objs, Action<object> action)
  32. {
  33. foreach (var o in objs)
  34. {
  35. action(o);
  36. }
  37. }
  38. /// <summary>
  39. /// 遍历集合
  40. /// </summary>
  41. /// <param name="objs"></param>
  42. /// <param name="action">回调方法</param>
  43. public static void ForEach(this IList<dynamic> objs, Action<object> action)
  44. {
  45. foreach (var o in objs)
  46. {
  47. action(o);
  48. }
  49. }
  50. /// <summary>
  51. /// 遍历数组
  52. /// </summary>
  53. /// <param name="objs"></param>
  54. /// <param name="action">回调方法</param>
  55. /// <typeparam name="T"></typeparam>
  56. public static void ForEach<T>(this T[] objs, Action<T> action)
  57. {
  58. foreach (var o in objs)
  59. {
  60. action(o);
  61. }
  62. }
  63. /// <summary>
  64. /// 遍历IEnumerable
  65. /// </summary>
  66. /// <param name="objs"></param>
  67. /// <param name="action">回调方法</param>
  68. /// <typeparam name="T"></typeparam>
  69. public static void ForEach<T>(this IEnumerable<T> objs, Action<T> action)
  70. {
  71. foreach (var o in objs)
  72. {
  73. action(o);
  74. }
  75. }
  76. /// <summary>
  77. /// 遍历List
  78. /// </summary>
  79. /// <param name="objs"></param>
  80. /// <param name="action">回调方法</param>
  81. /// <typeparam name="T"></typeparam>
  82. public static void ForEach<T>(this IList<T> objs, Action<T> action)
  83. {
  84. foreach (var o in objs)
  85. {
  86. action(o);
  87. }
  88. }
  89. /// <summary>
  90. /// 遍历数组并返回一个新的List
  91. /// </summary>
  92. /// <param name="objs"></param>
  93. /// <param name="action">回调方法</param>
  94. /// <returns></returns>
  95. public static IEnumerable<T> ForEach<T>(this object[] objs, Func<object, T> action)
  96. {
  97. foreach (var o in objs)
  98. {
  99. yield return action(o);
  100. }
  101. }
  102. /// <summary>
  103. /// 遍历IEnumerable并返回一个新的List
  104. /// </summary>
  105. /// <param name="objs"></param>
  106. /// <param name="action">回调方法</param>
  107. /// <typeparam name="T"></typeparam>
  108. /// <returns></returns>
  109. public static IEnumerable<T> ForEach<T>(this IEnumerable<dynamic> objs, Func<object, T> action)
  110. {
  111. foreach (var o in objs)
  112. {
  113. yield return action(o);
  114. }
  115. }
  116. /// <summary>
  117. /// 遍历List并返回一个新的List
  118. /// </summary>
  119. /// <param name="objs"></param>
  120. /// <param name="action">回调方法</param>
  121. /// <typeparam name="T"></typeparam>
  122. /// <returns></returns>
  123. public static IEnumerable<T> ForEach<T>(this IList<dynamic> objs, Func<object, T> action)
  124. {
  125. foreach (var o in objs)
  126. {
  127. yield return action(o);
  128. }
  129. }
  130. /// <summary>
  131. /// 遍历数组并返回一个新的List
  132. /// </summary>
  133. /// <param name="objs"></param>
  134. /// <param name="action">回调方法</param>
  135. /// <typeparam name="T"></typeparam>
  136. /// <returns></returns>
  137. public static IEnumerable<T> ForEach<T>(this T[] objs, Func<T, T> action)
  138. {
  139. foreach (var o in objs)
  140. {
  141. yield return action(o);
  142. }
  143. }
  144. /// <summary>
  145. /// 遍历IEnumerable并返回一个新的List
  146. /// </summary>
  147. /// <param name="objs"></param>
  148. /// <param name="action">回调方法</param>
  149. /// <typeparam name="T"></typeparam>
  150. /// <returns></returns>
  151. public static IEnumerable<T> ForEach<T>(this IEnumerable<T> objs, Func<T, T> action)
  152. {
  153. foreach (var o in objs)
  154. {
  155. yield return action(o);
  156. }
  157. }
  158. /// <summary>
  159. /// 遍历List并返回一个新的List
  160. /// </summary>
  161. /// <param name="objs"></param>
  162. /// <param name="action">回调方法</param>
  163. /// <typeparam name="T"></typeparam>
  164. /// <returns></returns>
  165. public static IEnumerable<T> ForEach<T>(this IList<T> objs, Func<T, T> action)
  166. {
  167. foreach (var o in objs)
  168. {
  169. yield return action(o);
  170. }
  171. }
  172. #endregion
  173. #region AsyncForEach
  174. /// <summary>
  175. /// 遍历数组
  176. /// </summary>
  177. /// <param name="objs"></param>
  178. /// <param name="action">回调方法</param>
  179. public static async void ForEachAsync(this object[] objs, Action<object> action)
  180. {
  181. await Task.Run(() =>
  182. {
  183. foreach (var o in objs)
  184. {
  185. action(o);
  186. }
  187. });
  188. }
  189. /// <summary>
  190. /// 遍历IEnumerable
  191. /// </summary>
  192. /// <param name="objs"></param>
  193. /// <param name="action">回调方法</param>
  194. public static async void ForEachAsync(this IEnumerable<dynamic> objs, Action<object> action)
  195. {
  196. await Task.Run(() =>
  197. {
  198. foreach (var o in objs)
  199. {
  200. action(o);
  201. }
  202. });
  203. }
  204. /// <summary>
  205. /// 遍历集合
  206. /// </summary>
  207. /// <param name="objs"></param>
  208. /// <param name="action">回调方法</param>
  209. public static async void ForEachAsync(this IList<dynamic> objs, Action<object> action)
  210. {
  211. await Task.Run(() =>
  212. {
  213. foreach (var o in objs)
  214. {
  215. action(o);
  216. }
  217. });
  218. }
  219. /// <summary>
  220. /// 遍历数组
  221. /// </summary>
  222. /// <param name="objs"></param>
  223. /// <param name="action">回调方法</param>
  224. /// <typeparam name="T"></typeparam>
  225. public static async void ForEachAsync<T>(this T[] objs, Action<T> action)
  226. {
  227. await Task.Run(() =>
  228. {
  229. foreach (var o in objs)
  230. {
  231. action(o);
  232. }
  233. });
  234. }
  235. /// <summary>
  236. /// 遍历IEnumerable
  237. /// </summary>
  238. /// <param name="objs"></param>
  239. /// <param name="action">回调方法</param>
  240. /// <typeparam name="T"></typeparam>
  241. public static async void ForEachAsync<T>(this IEnumerable<T> objs, Action<T> action)
  242. {
  243. await Task.Run(() =>
  244. {
  245. foreach (var o in objs)
  246. {
  247. action(o);
  248. }
  249. });
  250. }
  251. /// <summary>
  252. /// 遍历List
  253. /// </summary>
  254. /// <param name="objs"></param>
  255. /// <param name="action">回调方法</param>
  256. /// <typeparam name="T"></typeparam>
  257. public static async void ForEachAsync<T>(this IList<T> objs, Action<T> action)
  258. {
  259. await Task.Run(() =>
  260. {
  261. foreach (var o in objs)
  262. {
  263. action(o);
  264. }
  265. });
  266. }
  267. #endregion
  268. #region Map
  269. /// <summary>
  270. /// 映射到目标类型(浅克隆)
  271. /// </summary>
  272. /// <param name="source">源</param>
  273. /// <typeparam name="TDestination">目标类型</typeparam>
  274. /// <returns>目标类型</returns>
  275. public static TDestination MapTo<TDestination>(this object source) where TDestination : new()
  276. {
  277. TDestination dest = new TDestination();
  278. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source)); });
  279. return dest;
  280. }
  281. /// <summary>
  282. /// 映射到目标类型(浅克隆)
  283. /// </summary>
  284. /// <param name="source">源</param>
  285. /// <typeparam name="TDestination">目标类型</typeparam>
  286. /// <returns>目标类型</returns>
  287. public static async Task<TDestination> MapToAsync<TDestination>(this object source) where TDestination : new()
  288. {
  289. return await Task.Run(() =>
  290. {
  291. TDestination dest = new TDestination();
  292. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source)); });
  293. return dest;
  294. });
  295. }
  296. /// <summary>
  297. /// 映射到目标类型(深克隆)
  298. /// </summary>
  299. /// <param name="source">源</param>
  300. /// <typeparam name="TDestination">目标类型</typeparam>
  301. /// <returns>目标类型</returns>
  302. public static TDestination Map<TDestination>(this object source) where TDestination : new() => JsonConvert.DeserializeObject<TDestination>(JsonConvert.SerializeObject(source));
  303. /// <summary>
  304. /// 映射到目标类型(深克隆)
  305. /// </summary>
  306. /// <param name="source">源</param>
  307. /// <typeparam name="TDestination">目标类型</typeparam>
  308. /// <returns>目标类型</returns>
  309. public static async Task<TDestination> MapAsync<TDestination>(this object source) where TDestination : new() => await Task.Run(() => JsonConvert.DeserializeObject<TDestination>(JsonConvert.SerializeObject(source)));
  310. /// <summary>
  311. /// 复制一个新的对象
  312. /// </summary>
  313. /// <typeparam name="T"></typeparam>
  314. /// <param name="source"></param>
  315. /// <returns></returns>
  316. public static T Copy<T>(this T source) where T : new()
  317. {
  318. T dest = new T();
  319. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source)); });
  320. return dest;
  321. }
  322. /// <summary>
  323. /// 复制一个新的对象
  324. /// </summary>
  325. /// <typeparam name="T"></typeparam>
  326. /// <param name="source"></param>
  327. /// <returns></returns>
  328. public static async Task<T> CopyAsync<T>(this T source) where T : new() => await Task.Run(() =>
  329. {
  330. T dest = new T();
  331. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(source)); });
  332. return dest;
  333. });
  334. /// <summary>
  335. /// 映射到目标类型的集合
  336. /// </summary>
  337. /// <param name="source">源</param>
  338. /// <typeparam name="TDestination">目标类型</typeparam>
  339. /// <returns>目标类型集合</returns>
  340. public static IEnumerable<TDestination> ToList<TDestination>(this object[] source) where TDestination : new()
  341. {
  342. foreach (var o in source)
  343. {
  344. var dest = new TDestination();
  345. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
  346. yield return dest;
  347. }
  348. }
  349. /// <summary>
  350. /// 映射到目标类型的集合
  351. /// </summary>
  352. /// <param name="source">源</param>
  353. /// <typeparam name="TDestination">目标类型</typeparam>
  354. /// <returns>目标类型集合</returns>
  355. public static async Task<IEnumerable<TDestination>> ToListAsync<TDestination>(this object[] source) where TDestination : new()
  356. {
  357. return await Task.Run(() =>
  358. {
  359. IList<TDestination> list = new List<TDestination>();
  360. foreach (var o in source)
  361. {
  362. var dest = new TDestination();
  363. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
  364. list.Add(dest);
  365. }
  366. return list;
  367. });
  368. }
  369. /// <summary>
  370. /// 映射到目标类型的集合
  371. /// </summary>
  372. /// <param name="source">源</param>
  373. /// <typeparam name="TDestination">目标类型</typeparam>
  374. /// <returns>目标类型集合</returns>
  375. public static IEnumerable<TDestination> ToList<TDestination>(this IList<dynamic> source) where TDestination : new()
  376. {
  377. foreach (var o in source)
  378. {
  379. var dest = new TDestination();
  380. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
  381. yield return dest;
  382. }
  383. }
  384. /// <summary>
  385. /// 映射到目标类型的集合
  386. /// </summary>
  387. /// <param name="source">源</param>
  388. /// <typeparam name="TDestination">目标类型</typeparam>
  389. /// <returns>目标类型集合</returns>
  390. public static async Task<IEnumerable<TDestination>> ToListAsync<TDestination>(this IList<dynamic> source) where TDestination : new()
  391. {
  392. return await Task.Run(() =>
  393. {
  394. IList<TDestination> list = new List<TDestination>();
  395. foreach (var o in source)
  396. {
  397. var dest = new TDestination();
  398. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
  399. list.Add(dest);
  400. }
  401. return list;
  402. });
  403. }
  404. /// <summary>
  405. /// 映射到目标类型的集合
  406. /// </summary>
  407. /// <param name="source">源</param>
  408. /// <typeparam name="TDestination">目标类型</typeparam>
  409. /// <returns>目标类型集合</returns>
  410. public static IEnumerable<TDestination> ToList<TDestination>(this IEnumerable<dynamic> source) where TDestination : new()
  411. {
  412. foreach (var o in source)
  413. {
  414. var dest = new TDestination();
  415. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
  416. yield return dest;
  417. }
  418. }
  419. /// <summary>
  420. /// 映射到目标类型的集合
  421. /// </summary>
  422. /// <param name="source">源</param>
  423. /// <typeparam name="TDestination">目标类型</typeparam>
  424. /// <returns>目标类型集合</returns>
  425. public static async Task<IEnumerable<TDestination>> ToListAsync<TDestination>(this IEnumerable<dynamic> source) where TDestination : new()
  426. {
  427. return await Task.Run(() =>
  428. {
  429. IList<TDestination> list = new List<TDestination>();
  430. foreach (var o in source)
  431. {
  432. var dest = new TDestination();
  433. dest.GetType().GetProperties().ForEach(p => { p.SetValue(dest, source.GetType().GetProperty(p.Name)?.GetValue(o)); });
  434. list.Add(dest);
  435. }
  436. return list;
  437. });
  438. }
  439. #endregion
  440. /// <summary>
  441. /// 转换成json字符串
  442. /// </summary>
  443. /// <param name="source"></param>
  444. /// <returns></returns>
  445. public static string ToJsonString(this object source) => JsonConvert.SerializeObject(source);
  446. /// <summary>
  447. /// 转换成json字符串
  448. /// </summary>
  449. /// <param name="source"></param>
  450. /// <returns></returns>
  451. public static async Task<string> ToJsonStringAsync(this object source) => await Task.Run(() => JsonConvert.SerializeObject(source));
  452. #region UBB、HTML互转
  453. /// <summary>
  454. /// UBB代码处理函数
  455. /// </summary>
  456. /// <param name="ubbStr">输入UBB字符串</param>
  457. /// <returns>输出html字符串</returns>
  458. public static string UbbToHtml(this string ubbStr)
  459. {
  460. Regex r;
  461. Match m;
  462. #region 处理空格
  463. ubbStr = ubbStr.Replace(" ", "&nbsp;");
  464. #endregion
  465. #region 处理&符
  466. ubbStr = ubbStr.Replace("&", "&amp;");
  467. #endregion
  468. #region 处理单引号
  469. ubbStr = ubbStr.Replace("'", "’");
  470. #endregion
  471. #region 处理双引号
  472. ubbStr = ubbStr.Replace("\"", "&quot;");
  473. #endregion
  474. #region html标记符
  475. ubbStr = ubbStr.Replace("<", "&lt;");
  476. ubbStr = ubbStr.Replace(">", "&gt;");
  477. #endregion
  478. #region 处理换行
  479. //处理换行,在每个新行的前面添加两个全角空格
  480. r = new Regex(@"(\r\n((&nbsp;)| )+)(?<正文>\S+)", RegexOptions.IgnoreCase);
  481. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<BR>  " + m.Groups["正文"]);
  482. //处理换行,在每个新行的前面添加两个全角空格
  483. ubbStr = ubbStr.Replace("\r\n", "<BR>");
  484. #endregion
  485. #region 处[b][/b]标记
  486. r = new Regex(@"(\[b\])([ \S\t]*?)(\[\/b\])", RegexOptions.IgnoreCase);
  487. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<B>" + m.Groups[2] + "</B>");
  488. #endregion
  489. #region 处[i][/i]标记
  490. r = new Regex(@"(\[i\])([ \S\t]*?)(\[\/i\])", RegexOptions.IgnoreCase);
  491. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<I>" + m.Groups[2] + "</I>");
  492. #endregion
  493. #region 处[u][/u]标记
  494. r = new Regex(@"(\[U\])([ \S\t]*?)(\[\/U\])", RegexOptions.IgnoreCase);
  495. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<U>" + m.Groups[2] + "</U>");
  496. #endregion
  497. #region 处[p][/p]标记
  498. //处[p][/p]标记
  499. r = new Regex(@"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  500. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<P class=\"pstyle\">" + m.Groups[3] + "</P>");
  501. #endregion
  502. #region 处[sup][/sup]标记
  503. //处[sup][/sup]标记
  504. r = new Regex(@"(\[sup\])([ \S\t]*?)(\[\/sup\])", RegexOptions.IgnoreCase);
  505. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<SUP>" + m.Groups[2] + "</SUP>");
  506. #endregion
  507. #region 处[sub][/sub]标记
  508. //处[sub][/sub]标记
  509. r = new Regex(@"(\[sub\])([ \S\t]*?)(\[\/sub\])", RegexOptions.IgnoreCase);
  510. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<SUB>" + m.Groups[2] + "</SUB>");
  511. #endregion
  512. #region 处标记
  513. //处标记
  514. r = new Regex(@"(\[url\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
  515. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  516. {
  517. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[2] + "</A>");
  518. }
  519. #endregion
  520. #region 处[url=xxx][/url]标记
  521. //处[url=xxx][/url]标记
  522. r = new Regex(@"(\[url=([ \S\t]+)\])([ \S\t]*?)(\[\/url\])", RegexOptions.IgnoreCase);
  523. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  524. {
  525. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[3] + "</A>");
  526. }
  527. #endregion
  528. #region 处[email][/email]标记
  529. //处[email][/email]标记
  530. r = new Regex(@"(\[email\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
  531. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  532. {
  533. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"mailto:" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[2] + "</A>");
  534. }
  535. #endregion
  536. #region 处[email=xxx][/email]标记
  537. //处[email=xxx][/email]标记
  538. r = new Regex(@"(\[email=([ \S\t]+)\])([ \S\t]*?)(\[\/email\])", RegexOptions.IgnoreCase);
  539. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  540. {
  541. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"mailto:" + m.Groups[2] + "\" target=\"_blank\">" + m.Groups[3] + "</A>");
  542. }
  543. #endregion
  544. #region 处[size=x][/size]标记
  545. //处[size=x][/size]标记
  546. r = new Regex(@"(\[size=([1-7])\])([ \S\t]*?)(\[\/size\])", RegexOptions.IgnoreCase);
  547. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  548. {
  549. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<FONT SIZE=" + m.Groups[2] + ">" + m.Groups[3] + "</FONT>");
  550. }
  551. #endregion
  552. #region 处[color=x][/color]标记
  553. //处[color=x][/color]标记
  554. r = new Regex(@"(\[color=([\S]+)\])([ \S\t]*?)(\[\/color\])", RegexOptions.IgnoreCase);
  555. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  556. {
  557. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<FONT COLOR=" + m.Groups[2] + ">" + m.Groups[3] + "</FONT>");
  558. }
  559. #endregion
  560. #region 处[font=x][/font]标记
  561. //处[font=x][/font]标记
  562. r = new Regex(@"(\[font=([\S]+)\])([ \S\t]*?)(\[\/font\])", RegexOptions.IgnoreCase);
  563. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  564. {
  565. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<FONT FACE=" + m.Groups[2] + ">" + m.Groups[3] + "</FONT>");
  566. }
  567. #endregion
  568. #region 处理图片链接
  569. //处理图片链接
  570. r = new Regex("\\[picture\\](\\d+?)\\[\\/picture\\]", RegexOptions.IgnoreCase);
  571. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  572. {
  573. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<A href=\"ShowImage.aspx?Type=ALL&Action=forumImage&ImageID=" + m.Groups[1] + "\" target=\"_blank\"><IMG border=0 Title=\"点击打开新窗口查看\" src=\"ShowImage.aspx?Action=forumImage&ImageID=" + m.Groups[1] + "\"></A>");
  574. }
  575. #endregion
  576. #region 处理[align=x][/align]
  577. //处理[align=x][/align]
  578. r = new Regex(@"(\[align=([\S]+)\])([ \S\t]*?)(\[\/align\])", RegexOptions.IgnoreCase);
  579. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  580. {
  581. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<P align=" + m.Groups[2] + ">" + m.Groups[3] + "</P>");
  582. }
  583. #endregion
  584. #region 处[H=x][/H]标记
  585. //处[H=x][/H]标记
  586. r = new Regex(@"(\[H=([1-6])\])([ \S\t]*?)(\[\/H\])", RegexOptions.IgnoreCase);
  587. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  588. {
  589. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<H" + m.Groups[2] + ">" + m.Groups[3] + "</H" + m.Groups[2] + ">");
  590. }
  591. #endregion
  592. #region 处理[list=x][*][/list]
  593. //处理[list=x][*][/list]
  594. r = new Regex(@"(\[list(=(A|a|I|i| ))?\]([ \S\t]*)\r\n)((\[\*\]([ \S\t]*\r\n))*?)(\[\/list\])", RegexOptions.IgnoreCase);
  595. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  596. {
  597. string strLi = m.Groups[5].ToString();
  598. Regex rLi = new Regex(@"\[\*\]([ \S\t]*\r\n?)", RegexOptions.IgnoreCase);
  599. Match mLi;
  600. for (mLi = rLi.Match(strLi); mLi.Success; mLi = mLi.NextMatch()) strLi = strLi.Replace(mLi.Groups[0].ToString(), "<LI>" + mLi.Groups[1]);
  601. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<UL TYPE=\"" + m.Groups[3] + "\"><B>" + m.Groups[4] + "</B>" + strLi + "</UL>");
  602. }
  603. #endregion
  604. #region 处[SHADOW=x][/SHADOW]标记
  605. //处[SHADOW=x][/SHADOW]标记
  606. r = new Regex(@"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/SHADOW\])", RegexOptions.IgnoreCase);
  607. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  608. {
  609. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<TABLE WIDTH=" + m.Groups[2] + "STYLE=FILTER:SHADOW(COLOR=" + m.Groups[3] + ",STRENGTH=" + m.Groups[4] + ")>" + m.Groups[5] + "</TABLE>");
  610. }
  611. #endregion
  612. #region 处[glow=x][/glow]标记
  613. //处[glow=x][/glow]标记
  614. r = new Regex(@"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\S\t]*?)(\[\/glow\])", RegexOptions.IgnoreCase);
  615. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  616. {
  617. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<TABLE WIDTH=" + m.Groups[2] + " STYLE=FILTER:GLOW(COLOR=" + m.Groups[3] + ", STRENGTH=" + m.Groups[4] + ")>" + m.Groups[5] + "</TABLE>");
  618. }
  619. #endregion
  620. #region 处[center][/center]标记
  621. r = new Regex(@"(\[center\])([ \S\t]*?)(\[\/center\])", RegexOptions.IgnoreCase);
  622. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<CENTER>" + m.Groups[2] + "</CENTER>");
  623. #endregion
  624. #region 处[ IMG][ /IMG]标记
  625. r = new Regex(@"(\[IMG\])(http|https|ftp):\/\/([ \S\t]*?)(\[\/IMG\])", RegexOptions.IgnoreCase);
  626. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<br><a onfocus=this.blur() href=" + m.Groups[2] + "://" + m.Groups[3] + " target=_blank><IMG SRC=" + m.Groups[2] + "://" + m.Groups[3] + " border=0 alt=按此在新窗口浏览图片 onload=javascript:if(screen.width-333<this.width)this.width=screen.width-333></a>");
  627. #endregion
  628. #region 处[em]标记
  629. r = new Regex(@"(\[em([\S\t]*?)\])", RegexOptions.IgnoreCase);
  630. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<img src=pic/em" + m.Groups[2] + ".gif border=0 align=middle>");
  631. #endregion
  632. #region 处[flash=x][/flash]标记
  633. //处[mp=x][/mp]标记
  634. r = new Regex(@"(\[flash=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/flash\])", RegexOptions.IgnoreCase);
  635. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  636. {
  637. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<a href=" + m.Groups[4] + " TARGET=_blank><IMG SRC=pic/swf.gif border=0 alt=点击开新窗口欣赏该FLASH动画!> [全屏欣赏]</a><br><br><OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=" + m.Groups[2] + " height=" + m.Groups[3] + "><PARAM NAME=movie VALUE=" + m.Groups[4] + "><PARAM NAME=quality VALUE=high><param name=menu value=false><embed src=" + m.Groups[4] + " quality=high menu=false pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=" + m.Groups[2] + " height=" + m.Groups[3] + ">" + m.Groups[4] + "</embed></OBJECT>");
  638. }
  639. #endregion
  640. #region 处[dir=x][/dir]标记
  641. //处[dir=x][/dir]标记
  642. r = new Regex(@"(\[dir=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/dir\])", RegexOptions.IgnoreCase);
  643. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  644. {
  645. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=" + m.Groups[2] + " height=" + m.Groups[3] + "><param name=src value=" + m.Groups[4] + "><embed src=" + m.Groups[4] + " pluginspage=http://www.macromedia.com/shockwave/download/ width=" + m.Groups[2] + " height=" + m.Groups[3] + "></embed></object>");
  646. }
  647. #endregion
  648. #region 处[rm=x][/rm]标记
  649. //处[rm=x][/rm]标记
  650. r = new Regex(@"(\[rm=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/rm\])", RegexOptions.IgnoreCase);
  651. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  652. {
  653. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=" + m.Groups[2] + " height=" + m.Groups[3] + "><PARAM NAME=SRC VALUE=" + m.Groups[4] + "><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=" + m.Groups[2] + "><PARAM NAME=SRC VALUE=" + m.Groups[4] + "><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
  654. }
  655. #endregion
  656. #region 处[mp=x][/mp]标记
  657. //处[mp=x][/mp]标记
  658. r = new Regex(@"(\[mp=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/mp\])", RegexOptions.IgnoreCase);
  659. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  660. {
  661. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=" + m.Groups[2] + " height=" + m.Groups[3] + " ><param name=ShowStatusBar value=-1><param name=Filename value=" + m.Groups[4] + "><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=" + m.Groups[4] + " width=" + m.Groups[2] + " height=" + m.Groups[3] + "></embed></object>");
  662. }
  663. #endregion
  664. #region 处[qt=x][/qt]标记
  665. //处[qt=x][/qt]标记
  666. r = new Regex(@"(\[qt=)(\d*?),(\d*?)\]([\S\t]*?)(\[\/qt\])", RegexOptions.IgnoreCase);
  667. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  668. {
  669. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<embed src=" + m.Groups[4] + " width=" + m.Groups[2] + " height=" + m.Groups[3] + " autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
  670. }
  671. #endregion
  672. #region 处[QUOTE][/QUOTE]标记
  673. r = new Regex(@"(\[QUOTE\])([ \S\t]*?)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
  674. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<div style='border:#CCCCCC 1px dashed; width:94%; color:#999999; padding:3px; background:#F8F8F8'>" + m.Groups[2] + "</div><br /> ");
  675. #endregion
  676. #region 处[move][/move]标记
  677. r = new Regex(@"(\[move\])([ \S\t]*?)(\[\/move\])", RegexOptions.IgnoreCase);
  678. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<MARQUEE scrollamount=3>" + m.Groups[2] + "</MARQUEE>");
  679. #endregion
  680. #region 处[FLY][/FLY]标记
  681. r = new Regex(@"(\[FLY\])([ \S\t]*?)(\[\/FLY\])", RegexOptions.IgnoreCase);
  682. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch()) ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<MARQUEE width=80% behavior=alternate scrollamount=3>" + m.Groups[2] + "</MARQUEE>");
  683. #endregion
  684. #region 处[image][/image]标记
  685. //处[image][/image]标记
  686. r = new Regex(@"(\[image\])([ \S\t]*?)(\[\/image\])", RegexOptions.IgnoreCase);
  687. for (m = r.Match(ubbStr); m.Success; m = m.NextMatch())
  688. {
  689. ubbStr = ubbStr.Replace(m.Groups[0].ToString(), "<img src=\"" + m.Groups[2] + "\" border=0 align=middle><br>");
  690. }
  691. #endregion
  692. return ubbStr;
  693. }
  694. /// <summary>
  695. /// UBB代码处理函数
  696. /// </summary>
  697. /// <param name="ubbStr">输入UBB字符串</param>
  698. /// <returns>输出html字符串</returns>
  699. public static async Task<string> UbbToHtmlAsync(this string ubbStr) => await Task.Run(() => UbbToHtml(ubbStr));
  700. /// <summary>
  701. /// UBB转HTML方式2
  702. /// </summary>
  703. /// <param name="ubbStr">UBB 代码</param>
  704. /// <returns>HTML代码</returns>
  705. public static string UbbToHtml2(this string ubbStr)
  706. {
  707. ubbStr = ubbStr.Replace("&", "&amp;");
  708. ubbStr = ubbStr.Replace("<", "&lt;");
  709. ubbStr = ubbStr.Replace(">", "&gt;");
  710. ubbStr = ubbStr.Replace(" ", "&nbsp;"); //空格
  711. ubbStr = ubbStr.Replace("\n", "<br>"); //回车
  712. Regex my = new Regex(@"(\[IMG\])(.[^\[]*)(\[\/IMG\])", RegexOptions.IgnoreCase);
  713. ubbStr = my.Replace(ubbStr, @"<a href=""$2"" target=_blank><IMG SRC=""$2"" border=0 alt=按此在新窗口浏览图片 onload=""javascript:if(this.width>screen.width-333)this.width=screen.width-333""></a>");
  714. my = new Regex(@"\[DIR=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/DIR]", RegexOptions.IgnoreCase);
  715. ubbStr = my.Replace(ubbStr, @"<object classid=clsid:166B1BCA-3F9C-11CF-8075-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=7,0,2,0 width=$1 height=$2><param name=src value=$3><embed src=$3 pluginspage=http://www.macromedia.com/shockwave/download/ width=$1 height=$2></embed></object>");
  716. my = new Regex(@"\[QT=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/QT]", RegexOptions.IgnoreCase);
  717. ubbStr = my.Replace(ubbStr, @"<embed src=$3 width=$1 height=$2 autoplay=true loop=false controller=true playeveryframe=false cache=false scale=TOFIT bgcolor=#000000 kioskmode=false targetcache=false pluginspage=http://www.apple.com/quicktime/>");
  718. my = new Regex(@"\[MP=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/MP]", RegexOptions.IgnoreCase);
  719. ubbStr = my.Replace(ubbStr, @"<object align=middle classid=CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95 class=OBJECT id=MediaPlayer width=$1 height=$2 ><param name=ShowStatusBar value=-1><param name=Filename value=$3><embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=$3 width=$1 height=$2></embed></object>");
  720. my = new Regex(@"\[RM=*([0-9]*),*([0-9]*)\](.[^\[]*)\[\/RM]", RegexOptions.IgnoreCase);
  721. ubbStr = my.Replace(ubbStr, @"<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$1 height=$2><PARAM NAME=SRC VALUE=$3><PARAM NAME=CONSOLE VALUE=Clip1><PARAM NAME=CONTROLS VALUE=imagewindow><PARAM NAME=AUTOSTART VALUE=true></OBJECT><br><OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$1><PARAM NAME=SRC VALUE=$3><PARAM NAME=AUTOSTART VALUE=-1><PARAM NAME=CONTROLS VALUE=controlpanel><PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>");
  722. my = new Regex(@"(\[FLASH\])(.[^\[]*)(\[\/FLASH\])", RegexOptions.IgnoreCase);
  723. ubbStr = my.Replace(ubbStr, @"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$2""><PARAM NAME=quality VALUE=high><embed src=""$2"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$2</embed></OBJECT>");
  724. my = new Regex(@"(\[ZIP\])(.[^\[]*)(\[\/ZIP\])", RegexOptions.IgnoreCase);
  725. ubbStr = my.Replace(ubbStr, @"<br><IMG SRC=pic/zip.gif border=0> <a href=""$2"">点击下载该文件</a>");
  726. my = new Regex(@"(\[RAR\])(.[^\[]*)(\[\/RAR\])", RegexOptions.IgnoreCase);
  727. ubbStr = my.Replace(ubbStr, @"<br><IMG SRC=pic/rar.gif border=0> <a href=""$2"">点击下载该文件</a>");
  728. my = new Regex(@"(\[UPLOAD=(.[^\[]*)\])(.[^\[]*)(\[\/UPLOAD\])", RegexOptions.IgnoreCase);
  729. ubbStr = my.Replace(ubbStr, @"<br><IMG SRC=""pic/$2.gif"" border=0>此主题相关图片如下:<br><A HREF=""$3"" TARGET=_blank><IMG SRC=""$3"" border=0 alt=按此在新窗口浏览图片 onload=""javascript:if(this.width>screen.width-333)this.width=screen.width-333""></A>");
  730. my = new Regex(@"(\[URL\])(http:\/\/.[^\[]*)(\[\/URL\])", RegexOptions.IgnoreCase);
  731. ubbStr = my.Replace(ubbStr, @"<A HREF=""$2"" TARGET=_blank>$2</A>");
  732. my = new Regex(@"(\[URL\])(.[^\[]*)(\[\/URL\])", RegexOptions.IgnoreCase);
  733. ubbStr = my.Replace(ubbStr, @"<A HREF=""http://$2"" TARGET=_blank>$2</A>");
  734. my = new Regex(@"(\[URL=(http:\/\/.[^\[]*)\])(.[^\[]*)(\[\/URL\])", RegexOptions.IgnoreCase);
  735. ubbStr = my.Replace(ubbStr, @"<A HREF=""$2"" TARGET=_blank>$3</A>");
  736. my = new Regex(@"(\[URL=(.[^\[]*)\])(.[^\[]*)(\[\/URL\])", RegexOptions.IgnoreCase);
  737. ubbStr = my.Replace(ubbStr, @"<A HREF=""http://$2"" TARGET=_blank>$3</A>");
  738. my = new Regex(@"(\[EMAIL\])(\S+\@.[^\[]*)(\[\/EMAIL\])", RegexOptions.IgnoreCase);
  739. ubbStr = my.Replace(ubbStr, @"<A HREF=""mailto:$2"">$2</A>");
  740. my = new Regex(@"(\[EMAIL=(\S+\@.[^\[]*)\])(.[^\[]*)(\[\/EMAIL\])", RegexOptions.IgnoreCase);
  741. ubbStr = my.Replace(ubbStr, @"<A HREF=""mailto:$2"" TARGET=_blank>$3</A>");
  742. my = new Regex(@"^(HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  743. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  744. my = new Regex(@"(HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$", RegexOptions.IgnoreCase);
  745. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  746. my = new Regex(@"[^>=""](HTTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  747. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  748. my = new Regex(@"^(FTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  749. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  750. my = new Regex(@"(FTP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$", RegexOptions.IgnoreCase);
  751. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  752. my = new Regex(@"[^>=""](FTP://[A-Za-z0-9\.\/=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  753. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  754. my = new Regex(@"^(RTSP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  755. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  756. my = new Regex(@"(RTSP://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$", RegexOptions.IgnoreCase);
  757. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  758. my = new Regex(@"[^>=""](RTSP://[A-Za-z0-9\.\/=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  759. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  760. my = new Regex(@"^(MMS://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  761. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  762. my = new Regex(@"(MMS://[A-Za-z0-9\./=\?%\-&_~`@':+!]+)$", RegexOptions.IgnoreCase);
  763. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  764. my = new Regex(@"[^>=""](MMS://[A-Za-z0-9\.\/=\?%\-&_~`@':+!]+)", RegexOptions.IgnoreCase);
  765. ubbStr = my.Replace(ubbStr, @"<a target=_blank href=$1>$1</a>");
  766. my = new Regex(@"(\[HTML\])(.[^\[]*)(\[\/HTML\])", RegexOptions.IgnoreCase);
  767. ubbStr = my.Replace(ubbStr, @"<table width='100%' border='0' cellspacing='0' cellpadding='6' bgcolor=''><td><b>以下内容为程序代码:</b><br>$2</td></table>");
  768. my = new Regex(@"(\[CODE\])(.[^\[]*)(\[\/CODE\])", RegexOptions.IgnoreCase);
  769. ubbStr = my.Replace(ubbStr, @"<table width='100%' border='0' cellspacing='0' cellpadding='6' bgcolor=''><td><b>以下内容为程序代码:</b><br>$2</td></table>");
  770. my = new Regex(@"(\[COLOR=(.[^\[]*)\])(.[^\[]*)(\[\/COLOR\])", RegexOptions.IgnoreCase);
  771. ubbStr = my.Replace(ubbStr, @"<font COLOR=$2>$3</font>");
  772. my = new Regex(@"(\[FACE=(.[^\[]*)\])(.[^\[]*)(\[\/FACE\])", RegexOptions.IgnoreCase);
  773. ubbStr = my.Replace(ubbStr, @"<font FACE=$2>$3</font>");
  774. my = new Regex(@"(\[ALIGN=(.[^\[]*)\])(.*)(\[\/ALIGN\])", RegexOptions.IgnoreCase);
  775. ubbStr = my.Replace(ubbStr, @"<div ALIGN=$2>$3</div>");
  776. my = new Regex(@"(\[QUOTE\])(.*)(\[\/QUOTE\])", RegexOptions.IgnoreCase);
  777. ubbStr = my.Replace(ubbStr, @"<table cellpadding=0 cellspacing=0 border=0 WIDTH=94% bgcolor=#000000 align=center><tr><td><table width=100% cellpadding=5 cellspacing=1 border=0><TR><TD BGCOLOR=''>$2</table></table><br>");
  778. my = new Regex(@"(\[MOVE\])(.*)(\[\/MOVE\])", RegexOptions.IgnoreCase);
  779. ubbStr = my.Replace(ubbStr, @"<MARQUEE scrollamount=3>$2</marquee>");
  780. my = new Regex(@"\[GLOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/GLOW]", RegexOptions.IgnoreCase);
  781. ubbStr = my.Replace(ubbStr, @"<table width=$1 style=""filter:glow(color=$2, strength=$3)"">$4</table>");
  782. my = new Regex(@"\[SHADOW=*([0-9]*),*(#*[a-z0-9]*),*([0-9]*)\](.[^\[]*)\[\/SHADOW]", RegexOptions.IgnoreCase);
  783. ubbStr = my.Replace(ubbStr, @"<table width=$1 style=""filter:shadow(color=$2, strength=$3)"">$4</table>");
  784. my = new Regex(@"(\[I\])(.[^\[]*)(\[\/I\])", RegexOptions.IgnoreCase);
  785. ubbStr = my.Replace(ubbStr, @"<i>$2</i>");
  786. my = new Regex(@"(\[B\])(.[^\[]*)(\[\/U\])", RegexOptions.IgnoreCase);
  787. ubbStr = my.Replace(ubbStr, @"<u>$2</u>");
  788. my = new Regex(@"(\[B\])(.[^\[]*)(\[\/B\])", RegexOptions.IgnoreCase);
  789. ubbStr = my.Replace(ubbStr, @"<b>$2</b>");
  790. my = new Regex(@"(\[FLY\])(.[^\[]*)(\[\/FLY\])", RegexOptions.IgnoreCase);
  791. ubbStr = my.Replace(ubbStr, @"<marquee onmouseover='this.stop();' onmouseout='this.start();'>$2</marquee>");
  792. my = new Regex(@"(\[SIZE=1\])(.[^\[]*)(\[\/SIZE\])", RegexOptions.IgnoreCase);
  793. ubbStr = my.Replace(ubbStr, @"<font size=1>$2</font>");
  794. my = new Regex(@"(\[SIZE=2\])(.[^\[]*)(\[\/SIZE\])", RegexOptions.IgnoreCase);
  795. ubbStr = my.Replace(ubbStr, @"<font size=2>$2</font>");
  796. my = new Regex(@"(\[SIZE=3\])(.[^\[]*)(\[\/SIZE\])", RegexOptions.IgnoreCase);
  797. ubbStr = my.Replace(ubbStr, @"<font size=3>$2</font>");
  798. my = new Regex(@"(\[SIZE=4\])(.[^\[]*)(\[\/SIZE\])", RegexOptions.IgnoreCase);
  799. ubbStr = my.Replace(ubbStr, @"<font size=4>$2</font>");
  800. my = new Regex(@"(\[CENTER\])(.[^\[]*)(\[\/CENTER\])", RegexOptions.IgnoreCase);
  801. ubbStr = my.Replace(ubbStr, @"<center>$2</center>");
  802. return ubbStr;
  803. }
  804. /// <summary>
  805. /// UBB转HTML方式2
  806. /// </summary>
  807. /// <param name="ubbStr">UBB 代码</param>
  808. /// <returns>HTML代码</returns>
  809. public static async Task<string> UbbToHtml2Async(this string ubbStr) => await Task.Run(() => UbbToHtml2(ubbStr));
  810. /// <summary>
  811. /// Html转UBB
  812. /// </summary>
  813. /// <param name="chr">HTML代码</param>
  814. /// <returns>UBB代码</returns>
  815. public static string HtmltoUBB(this string chr)
  816. {
  817. if (chr == null) return "";
  818. chr = chr.Replace("&lt", "<");
  819. chr = chr.Replace("&gt", ">");
  820. chr = chr.Replace("<br/>", " ");
  821. chr = Regex.Replace(chr, @"<a href=$1 target=_blank>$2</a>", @"[url=(?<x>[^]]*)](?<y>[^]]*)[/url]", RegexOptions.IgnoreCase);
  822. chr = Regex.Replace(chr, @"<a href=$1 target=_blank>$1</a>", @"[url](?<x>[^]]*)[/url]", RegexOptions.IgnoreCase);
  823. chr = Regex.Replace(chr, @"<a href=$1>$2</a>", @"[email=(?<x>[^]]*)](?<y>[^]]*)[/email]", RegexOptions.IgnoreCase);
  824. chr = Regex.Replace(chr, @"<a href=$1>$1</a>", @"[email](?<x>[^]]*)[/email]", RegexOptions.IgnoreCase);
  825. chr = Regex.Replace(chr, @"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>", @"[flash](?<x>[^]]*)[/flash]", RegexOptions.IgnoreCase);
  826. chr = Regex.Replace(chr, @"<IMG SRC=""$1"" border=0>", @"[img](?<x>[^]]*)[/img]", RegexOptions.IgnoreCase);
  827. chr = Regex.Replace(chr, @"<font color=$1>$2</font>", @"[color=(?<x>[^]]*)](?<y>[^]]*)[/color]", RegexOptions.IgnoreCase);
  828. chr = Regex.Replace(chr, @"<font face=$1>$2</font>", @"[face=(?<x>[^]]*)](?<y>[^]]*)[/face]", RegexOptions.IgnoreCase);
  829. chr = Regex.Replace(chr, @"<font size=1>$1</font>", @"[size=1](?<x>[^]]*)[/size]", RegexOptions.IgnoreCase);
  830. chr = Regex.Replace(chr, @"<font size=2>$1</font>", @"[size=2](?<x>[^]]*)[/size]", RegexOptions.IgnoreCase);
  831. chr = Regex.Replace(chr, @"<font size=3>$1</font>", @"[size=3](?<x>[^]]*)[/size]", RegexOptions.IgnoreCase);
  832. chr = Regex.Replace(chr, @"<font size=4>$1</font>", @"[size=4](?<x>[^]]*)[/size]", RegexOptions.IgnoreCase);
  833. chr = Regex.Replace(chr, @"<align=$1>$2</align>", @"[align=(?<x>[^]]*)](?<y>[^]]*)[/align]", RegexOptions.IgnoreCase);
  834. chr = Regex.Replace(chr, @"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>", @"[fly](?<x>[^]]*)[/fly]", RegexOptions.IgnoreCase);
  835. chr = Regex.Replace(chr, @"<marquee scrollamount=3>$1</marquee>", @"[move](?<x>[^]]*)[/move]", RegexOptions.IgnoreCase);
  836. chr = Regex.Replace(chr, @"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>", @"[glow=(?<x>[^]]*),(?<y>[^]]*),(?<z>[^]]*)](?<w>[^]]*)[/glow]", RegexOptions.IgnoreCase);
  837. chr = Regex.Replace(chr, @"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>", @"[shadow=(?<x>[^]]*),(?<y>[^]]*),(?<z>[^]]*)](?<w>[^]]*)[/shadow]", RegexOptions.IgnoreCase);
  838. chr = Regex.Replace(chr, @"<b>$1</b>", @"[b](?<x>[^]]*)[/b]", RegexOptions.IgnoreCase);
  839. chr = Regex.Replace(chr, @"<i>$1</i>", @"[i](?<x>[^]]*)[/i]", RegexOptions.IgnoreCase);
  840. chr = Regex.Replace(chr, @"<u>$1</u>", @"[u](?<x>[^]]*)[/u]", RegexOptions.IgnoreCase);
  841. chr = Regex.Replace(chr, @"<pre id=code><font size=1 face='Verdana, Arial' id=code>$1</font id=code></pre id=code>", @"[code](?<x>[^]]*)[/code]", RegexOptions.IgnoreCase);
  842. chr = Regex.Replace(chr, @"<ul>$1</ul>", @"[list](?<x>[^]]*)[/list]", RegexOptions.IgnoreCase);
  843. chr = Regex.Replace(chr, @"<ol type=1>$1</ol id=1>", @"[list=1](?<x>[^]]*)[/list]", RegexOptions.IgnoreCase);
  844. chr = Regex.Replace(chr, @"<ol type=a>$1</ol id=a>", @"[list=a](?<x>[^]]*)[/list]", RegexOptions.IgnoreCase);
  845. chr = Regex.Replace(chr, @"<li>$1</li>", @"[*](?<x>[^]]*)[/*]", RegexOptions.IgnoreCase);
  846. chr = Regex.Replace(chr, @"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0' ><tr><td>$1</td></tr></table></center>", @"[quote](?<x>.*)[/quote]", RegexOptions.IgnoreCase);
  847. return chr;
  848. }
  849. /// <summary>
  850. /// Html转UBB
  851. /// </summary>
  852. /// <param name="chr">HTML代码</param>
  853. /// <returns>UBB代码</returns>
  854. public static async Task<string> HtmltoUBBAsync(this string chr) => await Task.Run(() => HtmltoUBB(chr));
  855. #endregion
  856. #region 数字互转
  857. /// <summary>
  858. /// 字符串转int
  859. /// </summary>
  860. /// <param name="s">源字符串</param>
  861. /// <returns>int类型的数字</returns>
  862. public static int ToInt32(this string s)
  863. {
  864. try
  865. {
  866. return Convert.ToInt32(s);
  867. }
  868. catch
  869. {
  870. return 0;
  871. }
  872. }
  873. /// <summary>
  874. /// 字符串转long
  875. /// </summary>
  876. /// <param name="s">源字符串</param>
  877. /// <returns>int类型的数字</returns>
  878. public static long ToInt64(this string s)
  879. {
  880. try
  881. {
  882. return Convert.ToInt64(s);
  883. }
  884. catch
  885. {
  886. return 0;
  887. }
  888. }
  889. /// <summary>
  890. /// 字符串转double
  891. /// </summary>
  892. /// <param name="s">源字符串</param>
  893. /// <returns>double类型的数据</returns>
  894. public static double ToDouble(this string s)
  895. {
  896. try
  897. {
  898. return Convert.ToDouble(s);
  899. }
  900. catch
  901. {
  902. return 0;
  903. }
  904. }
  905. /// <summary>
  906. /// 字符串转decimal
  907. /// </summary>
  908. /// <param name="s">源字符串</param>
  909. /// <returns>int类型的数字</returns>
  910. public static decimal ToDecimal(this string s)
  911. {
  912. try
  913. {
  914. return Convert.ToDecimal(s);
  915. }
  916. catch
  917. {
  918. return 0;
  919. }
  920. }
  921. /// <summary>
  922. /// 字符串转decimal
  923. /// </summary>
  924. /// <param name="s">源字符串</param>
  925. /// <returns>int类型的数字</returns>
  926. public static decimal ToDecimal(this double s)
  927. {
  928. try
  929. {
  930. return Convert.ToDecimal(s);
  931. }
  932. catch
  933. {
  934. return 0;
  935. }
  936. }
  937. /// <summary>
  938. /// 字符串转double
  939. /// </summary>
  940. /// <param name="s">源字符串</param>
  941. /// <returns>double类型的数据</returns>
  942. public static double ToDouble(this decimal s)
  943. {
  944. try
  945. {
  946. return Convert.ToDouble(s);
  947. }
  948. catch
  949. {
  950. return 0;
  951. }
  952. }
  953. /// <summary>
  954. /// 将double转换成int
  955. /// </summary>
  956. /// <param name="num">double类型</param>
  957. /// <returns>int类型</returns>
  958. public static int ToInt32(this double num)
  959. {
  960. return (int)Math.Floor(num);
  961. }
  962. /// <summary>
  963. /// 将double转换成int
  964. /// </summary>
  965. /// <param name="num">double类型</param>
  966. /// <returns>int类型</returns>
  967. public static int ToInt32(this decimal num)
  968. {
  969. return (int)Math.Floor(num);
  970. }
  971. /// <summary>
  972. /// 将int转换成double
  973. /// </summary>
  974. /// <param name="num">int类型</param>
  975. /// <returns>int类型</returns>
  976. public static double ToDouble(this int num)
  977. {
  978. return num * 1.0;
  979. }
  980. /// <summary>
  981. /// 将int转换成decimal
  982. /// </summary>
  983. /// <param name="num">int类型</param>
  984. /// <returns>int类型</returns>
  985. public static decimal ToDecimal(this int num)
  986. {
  987. return (decimal)(num * 1.0);
  988. }
  989. #endregion
  990. #region 检测字符串中是否包含列表中的关键词
  991. /// <summary>
  992. /// 检测字符串中是否包含列表中的关键词
  993. /// </summary>
  994. /// <param name="s">源字符串</param>
  995. /// <param name="keys">关键词列表</param>
  996. /// <returns></returns>
  997. public static bool Contains(this string s, string[] keys) => Regex.IsMatch(s.ToLower(), string.Join("|", keys).ToLower());
  998. #endregion
  999. #region 匹配Email
  1000. /// <summary>
  1001. /// 匹配Email
  1002. /// </summary>
  1003. /// <param name="s">源字符串</param>
  1004. /// <param name="isMatch">是否匹配成功,若返回true,则会得到一个Match对象,否则为null</param>
  1005. /// <returns>匹配对象</returns>
  1006. public static Match MatchEmail(this string s, out bool isMatch)
  1007. {
  1008. Match match = Regex.Match(s, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
  1009. isMatch = match.Success;
  1010. return isMatch ? match : null;
  1011. }
  1012. /// <summary>
  1013. /// 匹配Email
  1014. /// </summary>
  1015. /// <param name="s">源字符串</param>
  1016. /// <returns>是否匹配成功</returns>
  1017. public static bool MatchEmail(this string s)
  1018. {
  1019. MatchEmail(s, out bool success);
  1020. return success;
  1021. }
  1022. #endregion
  1023. #region 匹配完整的URL
  1024. /// <summary>
  1025. /// 匹配完整格式的URL,支持以下格式的URL,支持中文域名:<br/>
  1026. /// 支持不带协议名的网址,支持自适应协议的网址,支持完整路径,支持查询参数,支持锚点,支持锚点查询参数,支持16进制编码<br/>
  1027. /// www.baidu.com <br/>
  1028. /// www.baidu.com <br/>
  1029. /// baidu.com <br/>
  1030. /// //www.baidu.com <br/>
  1031. /// http://www.baidu.com <br/>
  1032. /// https://www.baidu.com <br/>
  1033. /// https://baidu.com <br/>
  1034. /// ftp://baidu.com <br/>
  1035. /// ftp://baidu.com/abc/def <br/>
  1036. /// ftp://admin:[email protected] <br/>
  1037. /// ftp://admin:[email protected]/abc/def <br/>
  1038. /// https://baidu.com:8080 <br/>
  1039. /// https://baidu.com/abc/def <br/>
  1040. /// https://baidu.com:8080/abc/def <br/>
  1041. /// https://baidu.com:8080/abc/def/hhh.html <br/>
  1042. /// https://baidu.com:8080/abc/def/hhh.html?s=www <br/>
  1043. /// https://baidu.com/abc/def/hhh.html?s=w@w{w}!s <br/>
  1044. /// https://baidu.com:8080/abc/def/hhh.html?s=www&amp;x=yy+y <br/>
  1045. /// https://baidu.com/abc/def/hhh.html?s=www&amp;x=yyy <br/>
  1046. /// https://baidu.com:8080/abc/def/hhh.html?s=www&amp;x=yyy#top <br/>
  1047. /// https://baidu.com:8080/abc/def/hi_jk-mn%ADF%AA/hhh.html?s=www&amp;x=yyy#top <br/>
  1048. /// https://baidu.com:8080/abc/def/hi_j+k-mn%ADF%AA?s=www&amp;x=yyy#top/aaa <br/>
  1049. /// https://baidu.com:8080/abc/def/hi_jk-mn%ADF%AA?s=www&amp;x=yyy#top/aaa/bbb/ccc <br/>
  1050. /// http://music.163.com/#/my/m/music/empty <br/>
  1051. /// http://music.163.com/abc/#/my/m/music/empty <br/>
  1052. /// http://music.163.com/def/hhh.html?s=www&amp;x=yyy#/my/m/music/empty <br/>
  1053. /// http://music.163.com/def/hhh.html?s=www&amp;x=yyy/#/my/m/music/empty <br/>
  1054. /// http://music.163.com/#/search/m/?%23%2Fmy%2Fm%2Fmusic%2Fempty=&amp;s=fade&amp;type=1!k <br/>
  1055. /// </summary>
  1056. /// <param name="s">源字符串</param>
  1057. /// <param name="isMatch">是否匹配成功,若返回true,则会得到一个Match对象,否则为null</param>
  1058. /// <returns>匹配对象</returns>
  1059. public static Match MatchUrl(this string s, out bool isMatch)
  1060. {
  1061. Match match = Regex.Match(s, @"^((\w*):?//)?((\w+)\.|((\S+):(\S+))@)?((\w+)\.(\w+))(:(\d{1,5}))?(/([a-z0-9A-Z-_@{}!+%/]+(\.\w+)?)?(\?([a-z0-9A-Z-_@{}!+%]+=[a-z0-9A-Z-_@{}!+%]+&?)+)?(/?#[a-z0-9A-Z-_@{}!+%/]+)?(\?([a-z0-9A-Z-_@{}!+%]+=[a-z0-9A-Z-_@{}!+%]*&?)+)?)?$");
  1062. isMatch = match.Success;
  1063. return isMatch ? match : null;
  1064. }
  1065. /// <summary>
  1066. /// 匹配完整格式的URL,支持以下格式的URL,支持中文域名:<br/>
  1067. /// 支持不带协议名的网址,支持自适应协议的网址,支持完整路径,支持查询参数,支持锚点,支持锚点查询参数,支持16进制编码<br/>
  1068. /// www.baidu.com <br/>
  1069. /// www.baidu.com <br/>
  1070. /// baidu.com <br/>
  1071. /// //www.baidu.com <br/>
  1072. /// http://www.baidu.com <br/>
  1073. /// https://www.baidu.com <br/>
  1074. /// https://baidu.com <br/>
  1075. /// ftp://baidu.com <br/>
  1076. /// ftp://baidu.com/abc/def <br/>
  1077. /// ftp://admin:[email protected] <br/>
  1078. /// ftp://admin:[email protected]/abc/def <br/>
  1079. /// https://baidu.com:8080 <br/>
  1080. /// https://baidu.com/abc/def <br/>
  1081. /// https://baidu.com:8080/abc/def <br/>
  1082. /// https://baidu.com:8080/abc/def/hhh.html <br/>
  1083. /// https://baidu.com:8080/abc/def/hhh.html?s=www <br/>
  1084. /// https://baidu.com/abc/def/hhh.html?s=w@w{w}!s <br/>
  1085. /// https://baidu.com:8080/abc/def/hhh.html?s=www&amp;x=yy+y <br/>
  1086. /// https://baidu.com/abc/def/hhh.html?s=www&amp;x=yyy <br/>
  1087. /// https://baidu.com:8080/abc/def/hhh.html?s=www&amp;x=yyy#top <br/>
  1088. /// https://baidu.com:8080/abc/def/hi_jk-mn%ADF%AA/hhh.html?s=www&amp;x=yyy#top <br/>
  1089. /// https://baidu.com:8080/abc/def/hi_j+k-mn%ADF%AA?s=www&amp;x=yyy#top/aaa <br/>
  1090. /// https://baidu.com:8080/abc/def/hi_jk-mn%ADF%AA?s=www&amp;x=yyy#top/aaa/bbb/ccc <br/>
  1091. /// http://music.163.com/#/my/m/music/empty <br/>
  1092. /// http://music.163.com/abc/#/my/m/music/empty <br/>
  1093. /// http://music.163.com/def/hhh.html?s=www&amp;x=yyy#/my/m/music/empty <br/>
  1094. /// http://music.163.com/def/hhh.html?s=www&amp;x=yyy/#/my/m/music/empty <br/>
  1095. /// http://music.163.com/#/search/m/?%23%2Fmy%2Fm%2Fmusic%2Fempty=&amp;s=fade&amp;type=1!k <br/>
  1096. /// </summary>
  1097. /// <param name="s">源字符串</param>
  1098. /// <returns>是否匹配成功</returns>
  1099. public static bool MatchUrl(this string s)
  1100. {
  1101. MatchUrl(s, out bool success);
  1102. return success;
  1103. }
  1104. #endregion
  1105. #region 权威校验身份证号码
  1106. /// <summary>
  1107. /// 根据GB11643-1999标准权威校验中国身份证号码的合法性
  1108. /// </summary>
  1109. /// <param name="s">源字符串</param>
  1110. /// <returns>是否匹配成功</returns>
  1111. public static bool MatchIdentifyCard(this string s)
  1112. {
  1113. if (s.Length == 18)
  1114. {
  1115. long n;
  1116. if (long.TryParse(s.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(s.Replace('x', '0').Replace('X', '0'), out n) == false)
  1117. {
  1118. return false; //数字验证
  1119. }
  1120. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  1121. if (address.IndexOf(s.Remove(2), StringComparison.Ordinal) == -1)
  1122. {
  1123. return false; //省份验证
  1124. }
  1125. string birth = s.Substring(6, 8).Insert(6, "-").Insert(4, "-");
  1126. DateTime time;
  1127. if (!DateTime.TryParse(birth, out time))
  1128. {
  1129. return false; //生日验证
  1130. }
  1131. string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  1132. string[] wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  1133. char[] ai = s.Remove(17).ToCharArray();
  1134. int sum = 0;
  1135. for (int i = 0; i < 17; i++)
  1136. {
  1137. sum += wi[i].ToInt32() * ai[i].ToString().ToInt32();
  1138. }
  1139. int y;
  1140. Math.DivRem(sum, 11, out y);
  1141. if (arrVarifyCode[y] != s.Substring(17, 1).ToLower())
  1142. {
  1143. return false; //校验码验证
  1144. }
  1145. return true; //符合GB11643-1999标准
  1146. }
  1147. if (s.Length == 15)
  1148. {
  1149. long n;
  1150. if (long.TryParse(s, out n) == false || n < Math.Pow(10, 14))
  1151. {
  1152. return false; //数字验证
  1153. }
  1154. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  1155. if (address.IndexOf(s.Remove(2), StringComparison.Ordinal) == -1)
  1156. {
  1157. return false; //省份验证
  1158. }
  1159. string birth = s.Substring(6, 6).Insert(4, "-").Insert(2, "-");
  1160. DateTime time;
  1161. if (DateTime.TryParse(birth, out time) == false)
  1162. {
  1163. return false; //生日验证
  1164. }
  1165. return true;
  1166. }
  1167. return false;
  1168. }
  1169. #endregion
  1170. #region 校验IP地址的合法性
  1171. /// <summary>
  1172. /// 校验IP地址的正确性,同时支持IPv4和IPv6
  1173. /// </summary>
  1174. /// <param name="s">源字符串</param>
  1175. /// <param name="isMatch">是否匹配成功,若返回true,则会得到一个Match对象,否则为null</param>
  1176. /// <returns>匹配对象</returns>
  1177. public static Match MatchInetAddress(this string s, out bool isMatch)
  1178. {
  1179. Match match;
  1180. if (s.Contains(":"))
  1181. {
  1182. //IPv6
  1183. match = Regex.Match(s, "^((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))$");
  1184. isMatch = match.Success;
  1185. }
  1186. else
  1187. {
  1188. //IPv4
  1189. match = Regex.Match(s, @"^(\d+)\.(\d+)\.(\d+)\.(\d+)$");
  1190. isMatch = match.Success;
  1191. foreach (Group m in match.Groups)
  1192. {
  1193. if (m.Value.ToInt32() < 0 || m.Value.ToInt32() > 255)
  1194. {
  1195. isMatch = false;
  1196. break;
  1197. }
  1198. }
  1199. }
  1200. return isMatch ? match : null;
  1201. }
  1202. /// <summary>
  1203. /// 校验IP地址的正确性,同时支持IPv4和IPv6
  1204. /// </summary>
  1205. /// <param name="s">源字符串</param>
  1206. /// <returns>是否匹配成功</returns>
  1207. public static bool MatchInetAddress(this string s)
  1208. {
  1209. MatchInetAddress(s, out bool success);
  1210. return success;
  1211. }
  1212. #endregion
  1213. #region 校验手机号码的正确性
  1214. /// <summary>
  1215. /// 匹配手机号码
  1216. /// </summary>
  1217. /// <param name="s">源字符串</param>
  1218. /// <param name="isMatch">是否匹配成功,若返回true,则会得到一个Match对象,否则为null</param>
  1219. /// <returns>匹配对象</returns>
  1220. public static Match MatchPhoneNumber(this string s, out bool isMatch)
  1221. {
  1222. Match match = Regex.Match(s, @"^((1[3,5,8][0-9])|(14[5,7])|(17[0,1,6,7,8]))\d{8}$");
  1223. isMatch = match.Success;
  1224. return isMatch ? match : null;
  1225. }
  1226. /// <summary>
  1227. /// 匹配手机号码
  1228. /// </summary>
  1229. /// <param name="s">源字符串</param>
  1230. /// <returns>是否匹配成功</returns>
  1231. public static bool MatchPhoneNumber(this string s)
  1232. {
  1233. MatchPhoneNumber(s, out bool success);
  1234. return success;
  1235. }
  1236. #endregion
  1237. /// <summary>
  1238. /// 严格比较两个对象是否是同一对象
  1239. /// </summary>
  1240. /// <param name="_this">自己</param>
  1241. /// <param name="o">需要比较的对象</param>
  1242. /// <returns>是否同一对象</returns>
  1243. public new static bool ReferenceEquals(this object _this, object o) => object.ReferenceEquals(_this, o);
  1244. /// <summary>
  1245. /// 判断字符串是否为空
  1246. /// </summary>
  1247. /// <param name="s"></param>
  1248. /// <returns></returns>
  1249. public static bool IsNullOrEmpty(this string s) => string.IsNullOrEmpty(s);
  1250. /// <summary>
  1251. /// 类型直转
  1252. /// </summary>
  1253. /// <typeparam name="T"></typeparam>
  1254. /// <param name="value"></param>
  1255. /// <returns></returns>
  1256. public static T To<T>(this IConvertible value)
  1257. {
  1258. try
  1259. {
  1260. return (T)Convert.ChangeType(value, typeof(T));
  1261. }
  1262. catch
  1263. {
  1264. return default(T);
  1265. }
  1266. }
  1267. /// <summary>
  1268. /// 字符串转时间
  1269. /// </summary>
  1270. /// <param name="value"></param>
  1271. /// <returns></returns>
  1272. public static DateTime ToDateTime(this string value)
  1273. {
  1274. try
  1275. {
  1276. return DateTime.Parse(value);
  1277. }
  1278. catch
  1279. {
  1280. return default(DateTime);
  1281. }
  1282. }
  1283. /// <summary>
  1284. /// 字符串转Guid
  1285. /// </summary>
  1286. /// <param name="s"></param>
  1287. /// <returns></returns>
  1288. public static Guid ToGuid(this string s)
  1289. {
  1290. return Guid.Parse(s);
  1291. }
  1292. /// <summary>
  1293. /// 根据正则替换
  1294. /// </summary>
  1295. /// <param name="input"></param>
  1296. /// <param name="regex">正则表达式</param>
  1297. /// <param name="replacement">新内容</param>
  1298. /// <returns></returns>
  1299. public static string Replace(this string input, Regex regex, string replacement)
  1300. {
  1301. return regex.Replace(input, replacement);
  1302. }
  1303. }
  1304. }