1
1

Extensions.cs 67 KB

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