Unity 正则表达式

Regex
正则表达式通常用来检查,检索,替换符合某个格式的文本

元字符:
正则表达式语言由两种基本字符组成.
原义(正常)文本字符和元字符.
元字符使用正则表达式具有处理能力.

   ^        表示开头
   $         表示结尾
   .         匹配除换行符以外的任意字符
   *         0 or more
   ?         0 or 1
   +         1 or more
  \w         表示字母,数字,下划线,汉字任意字符(指大小写字母,0-9的数字,下划线“_”)
  \W       \w 的补集
  \d         表示数字
  \D         表示非数字
  \s         表示字符串  
  \S         表示非空白字符
  [\s\S]     表示任意字符
  [\s\S]*     表示0到任意个字符
  [\s\S]*?   0个字符,匹配任意个字符前的次数
  [a-z]       表示某个范围内的字符,与指定区间内任何字符进行匹配
  [A-Z]   
  [0-9]
  \u4e00-\u9fa5   表示中文
  |           逻辑或
  ()         分组
  {n,m}       表示最少匹配n次,最高匹配m次
  {n,}       最少匹配n次
  {n}         匹配前面的字符最少n次
  [^X]       表示除了X外任意字符
using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace 正则表达式
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			//演示在开头处拼接
//			string str = "大家好";
//			string newStr = Regex.Replace (str, "^", "首长:");
//			Console.WriteLine (newStr);

			//演示在结尾处拼接
//			string str = "大家好";
//			string newStr = Regex.Replace (str, "$", ",首长");
//			Console.WriteLine (newStr);

			//以数字开头,中间有n个数字,以中文结尾.
//			string str = "010789你";
//			string pattern = @"\d*[\u4e00-\u9fa5]$";
//			if (Regex.IsMatch (str, pattern)) {
//				Console.WriteLine ("合法");
//			}else{
//				Console.WriteLine ("不合法");
//			}

			//匹配邮箱
			//xxxx@xxx.xx
//			string str = "zhaoqingyu@lanou3g.com.cn";
//			string pattern = @"\w+@\w+(\.\w+)+$";
//			if (Regex.IsMatch (str, pattern)) {
//				Console.WriteLine ("合法");
//			} else {
//				Console.WriteLine ("不合法");
//			}

			//匹配身份证号
			//身份证15位 或 18位
			//15位全数字
			//18位全数字
			//17位数字+X
//			string str = "11111111111111111B";   //18位
//			string pattern = @"(^\d{15,15}$)|(^\d{17}([0-9]|X)$)";
//			if (Regex.IsMatch (str, pattern)) {
//				Console.WriteLine ("合法");
//			} else {
//				Console.WriteLine ("不合法");
//			}

			//匹配座机
			//010-12345678
			//01012345678
			//(010)12345678
//			string tell = "010-12345678";
//			string tell = "01012345678";
//			string tell = "(010)-1234567";
//			string pattern = @"\(0\d{2,3}\)\d{7,8}$|^0\d{2,3}[-]?\d{7,8}$";
			string pattern = @"(^0\d{2,3}[1-9]\d{7,7}$)|(^0\d{2,3}-[1-9]\d{7,7}$)|(^\(0\d{2,3}\)[1-9]\d{7,7}$)";
//			if (Regex.IsMatch (tell, pattern)) {
//				Console.WriteLine ("合法");
//			} else {
//				Console.WriteLine ("不合法");
//			}

			//判断手机号
			//138xxxx
//			string str = "132456789012";
//			string pattern = @"^[1][3,5,7,8][0-9]{9}$";
//			if (Regex.IsMatch (str, pattern)) {
//				Console.WriteLine ("合法");
//			} else {
//				Console.WriteLine ("不合法");
//			}

			// 判断扣扣号
			string str = "1322085934";
			string pattern = @"^[1,2]\d{9}$";
			if (Regex.IsMatch (str, pattern)) {
				Console.WriteLine ("合法");
			} else {
				Console.WriteLine ("不合法");
			}

			//将#xxx#抽取出来
			//#1024#天啊,#带着钱一起跑路#带回家按客户的骄傲是多少贷
//			string str = "#1024#天啊,#带着钱一起跑路#带回家按客户的骄傲是多少贷";
//			string pattern = @"#[0-9a-zA-Z\u4e00-\u9fa5]+#";
//			MatchCollection mc = Regex.Matches (str, pattern);
//			Console.WriteLine (mc.Count);
//			foreach (Match item in mc) {
//				Console.WriteLine (item);
//			}


			//检索特殊字符
//			string str = "*$172638";
//			string pattern = @"[^\w\s]+";
//			MatchCollection mc  = Regex.Matches(str,pattern);
//			foreach (Match item in mc) {
//				Console.WriteLine (item);
//			}

			//敏感字眼检索
//			string str = "天下武功唯快不破";
//			if (MainClass.JudgeMessage (str)) {
//				Console.WriteLine ("消息发送失败");
//			} else {
//				//上传服务器
//				Console.WriteLine("消息发送成功");
//			}

		}

		public static bool JudgeMessage(string s){
			string pattern = @"[你好]|[您好]";
			return Regex.IsMatch (s, pattern);	
		}
	}
}
Unity 中使用正则表达式匹配换行符可以通过 `\r` 和 `\n` 这两个特殊字符来实现。以下是详细的说明以及示例代码: ### 使用正则表达式匹配换行符 #### 1. 单独匹配换行符 `\r` 表示回车符(Carriage Return),通常用于 Windows 文件格式; `\n` 表示换行符(Line Feed),通常用于 Unix 或 macOS 文件格式。 如果要单独匹配这两种换行符,可以分别编写如下正则表达式[^1]: - `Regex regexCR = new Regex(@"\r");` (匹配回车符) - `Regex regexLF = new Regex(@"\n");` (匹配换行符) #### 2. 同时匹配两种换行符组合 为了兼容不同操作系统的文件格式,推荐同时匹配 `\r\n` 组合或者单个 `\n`/`\r`。可以使用以下正则表达式[^1]: ```csharp string pattern = @"(\r\n|\r|\n)"; Regex regex = new Regex(pattern); ``` 此模式能够识别三种情况: - `\r\n`:Windows 风格的换行。 - `\r`:旧版 Macintosh 风格的换行。 - `\n`:Unix/Linux/macOS 风格的换行。 #### 3. 替换换行符 如果希望移除或替换所有的换行符,可以使用 `Replace` 方法[^2]。例如,将所有换行符替换成空格: ```csharp string input = "This is a test.\r\nAnother line."; string result = Regex.Replace(input, @"(\r\n|\r|\n)", " "); Debug.Log(result); // 输出: This is a test. Another line. ``` #### 4. 完整示例代码 下面是一个完整的 C# 脚本示例,展示如何检测并处理字符串中的换行符: ```csharp using UnityEngine; using System.Text.RegularExpressions; public class RegexExample : MonoBehaviour { void Start() { string input = "Hello,\r\nWorld!\nThis is a test."; // 创建正则表达式对象 string pattern = @"(\r\n|\r|\n)"; Regex regex = new Regex(pattern); // 查找第一个匹配项 Match match = regex.Match(input); if (match.Success) { Debug.Log($"Found newline character: {match.Value}"); } // 将所有换行符替换为空白 string replacedString = regex.Replace(input, " "); Debug.Log($"Replaced String: {replacedString}"); } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值