Extensions.cs 71 KB

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