Problem. You want to split strings on different characters with single character or string delimiters. For example, split a string that contains ""r"n" sequences, which are Windows newlines. Solution. This document contains several tips for the Split method on the string type in the C# programming language.
Input string: One,Two,Three,Four,Five
Delimiter: , (char)
Array: One (string array)
Two
Three
Four
Five
Here we see the basic Split method overload. You already know the general way to do this, but it is good to look at the basic syntax before we move on. This example splits on a single character.
=== Example program for splitting on spaces ===
using System;
class Program
{
static void Main()
{
string s = "there is a cat";
//
// Split string on spaces.
// This will separate all the words.
//
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
=== Output of the program ===
there
is
a
cat
Description. The input string, which contains four words, is split on spaces and the foreach loop then displays each word. The result value from Split is a string[] array.
Here we use either the Regex method or the C# new array syntax. Note that a new char array is created in the following usages. There is an overloaded method with that signature if you need StringSplitOptions, which is used to remove empty strings.
=== Program that splits on lines with Regex ===
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string value = "cat"r"ndog"r"nanimal"r"nperson";
//
// Split the string on line breaks.
// The return value from Split is a string[] array.
//
string[] lines = Regex.Split(value, ""r"n");
foreach (string line in lines)
{
Console.WriteLine(line);
}
}
}
=== Output of the program ===
cat
dog
animal
person
Description. The first example uses Regex. Regex contains the Split method, which is static. It can be used to split strings, although it has different performance properties. The next two example show how you can specify an array as the first parameter to string Split.
=== Program that splits on multiple characters ===
using System;
class Program
{
static void Main()
{
//
// This string is also separated by Windows line breaks.
//
string value = "shirt"r"ndress"r"npants"r"njacket";
//
// Use a new char[] array of two characters ("r and "n) to break
// lines from into separate strings. Use "RemoveEmptyEntries"
// to make sure no empty strings get put in the string[] array.
//
char[] delimiters = new char[] { '"r', '"n' };
string[] parts = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
Console.WriteLine(parts[i]);
}
//
// Same as the previous example, but uses a new string of 2 characters.
//
parts = value.Split(new string[] { ""r"n" }, StringSplitOptions.None);
for (int i = 0; i < parts.Length; i++)
{
Console.WriteLine(parts[i]);
}
}
}
=== Output of the program ===
(Repeated two times)
shirt
dress
pants
jacket
Overview. One useful overload of Split receives char[] arrays. The string Split method can receive a character array as the first parameter. Each char in the array designates a new block.
Using string arrays. Another overload of Split receives string[] arrays. This means string array can also be passed to the Split method. The new string[] array is created inline with the Split call.
Explanation of StringSplitOptions. The RemoveEmptyEntries enum is specified. When two delimiters are adjacent, we end up with an empty result. We can use this as the second parameter to avoid this. [C# StringSplitOptions Enumeration - dotnetperls.com] The following screenshot shows the Visual Studio debugger.
Here we see how you can separate words with Split. Usually, the best way to separate words is to use a Regex that specifies non-word chars. This example separates words in a string based on non-word characters. It eliminates punctuation and whitespace from the return array.
=== Program that separates on non-word pattern ===
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string[] w = SplitWords("That is a cute cat, man");
foreach (string s in w)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
/// <summary>
/// Take all the words in the input string and separate them.
/// </summary>
static string[] SplitWords(string s)
{
//
// Split on all non-word characters.
// Returns an array of all the words.
//
return Regex.Split(s, @""W+");
// @ special verbatim string syntax
// "W+ one or more non-word characters together
}
}
=== Output of the program ===
That
is
a
cute
cat
man
Word splitting example. Here you can separate parts of your input string based on any character set or range with Regex. Overall, this provides more power than the string Split methods. [C# Regex.Split Method Examples - dotnetperls.com]
Here you have a text file containing comma-delimited lines of values. This is called a CSV file, and it is easily dealt with in C#. We use the File.ReadAllLines method here, but you may want StreamReader instead.
Reading the following code. The C# code next reads in both of those lines, parses them, and displays the values of each line after the line number. The final comment shows how the file was parsed into the strings.
=== Contents of input file (TextFile1.txt) ===
Dog,Cat,Mouse,Fish,Cow,Horse,Hyena
Programmer,Wizard,CEO,Rancher,Clerk,Farmer
=== Program that splits lines in file (C#) ===
using System;
using System.IO;
class Program
{
static void Main()
{
int i = 0;
foreach (string line in File.ReadAllLines("TextFile1.txt"))
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
Console.WriteLine("{0}:{1}",
i,
part);
}
i++; // For demo only
}
}
}
=== Output of the program ===
0:Dog
0:Cat
0:Mouse
0:Fish
0:Cow
0:Horse
0:Hyena
1:Programmer
1:Wizard
1:CEO
1:Rancher
1:Clerk
1:Farmer
Here we see how you can Split the segments in a Windows local directory into separate strings. Note that directory paths are complex and this may not handle all cases correctly. It is also platform-specific, and you could use System.IO.Path. DirectorySeparatorChar for more flexibility. [C# Path Examples - dotnetperls.com]
=== Program that splits Windows directories (C#) ===
using System;
class Program
{
static void Main()
{
// The directory from Windows
const string dir = @"C:"Users"Sam"Documents"Perls"Main";
// Split on directory separator
string[] parts = dir.Split('""');
foreach (string part in parts)
{
Console.WriteLine(part);
}
}
}
=== Output of the program ===
C:
Users
Sam
Documents
Perls
Main
The logic internal to the .NET framework for Split is implemented in managed code. The methods call into the overload with three parameters. The parameters are next checked for validity. Finally, it uses unsafe code to create the separator list, and then a for loop combined with Substring to return the array.
The author tested a long string and a short string, having 40 and 1200 chars. String splitting speed varies on the type of strings. The length of the blocks, number of delimiters, and total size of the string factor into performance.
Results. The Regex.Split option generally performed the worst. The author felt that the second or third methods would be the best, after observing performance problems with regular expressions in other situations.
=== Strings used in test ===
//
// Build long string.
//
_test = string.Empty;
for (int i = 0; i < 120; i++)
{
_test += "01234567"r"n";
}
//
// Build short string.
//
_test = string.Empty;
for (int i = 0; i < 10; i++)
{
_test += "ab"r"n";
}
=== Example methods tested (100000 iterations) ===
static void Test1()
{
string[] arr = Regex.Split(_test, ""r"n", RegexOptions.Compiled);
}
static void Test2()
{
string[] arr = _test.Split(new char[] { '"r', '"n' }, StringSplitOptions.RemoveEmptyEntries);
}
static void Test3()
{
string[] arr = _test.Split(new string[] { ""r"n" }, StringSplitOptions.None);
}
Longer strings: 1200 chars. The benchmark for the methods on the long strings is more even. It may be that for very long strings, such as entire files, the Regex method is equivalent or even faster. For short strings, Regex is slowest, but for long strings it is very fast.
=== Benchmark of Split on long strings ===
[1] Regex.Split: 3470 ms
[2] char[] Split: 1255 ms [fastest]
[3] string[] Split: 1449 ms
=== Benchmark of Split on short strings ===
[1] Regex.Split: 434 ms
[2] char[] Split: 63 ms [fastest]
[3] string[] Split: 83 ms
Short strings: 40 chars. This shows the three methods compared to each other on short strings. Method 1 is the Regex method, and it is by far the slowest on the short strings. This may be because of the compilation time. Smaller is better. [This article was last updated for .NET 3.5 SP1.]
Performance recommendation. For programs that use shorter strings, the methods that split based on arrays are faster and simpler, and they will avoid Regex compilation. For somewhat longer strings or files that contain more lines, Regex is appropriate. I show some Split improvements that can improve your program. [C# Split Improvement - dotnetperls.com]
You can use Replace on your string input to substitute special characters in for any escaped characters. This can solve lots of problems on parsing computer-generated code or data. [C# Split Method and Escape Characters - dotnetperls.com]
The author's further research into Split and its performance shows that it is worthwhile to declare your char[] array you are splitting on as a local instance to reduce memory pressure and improve runtime performance.
=== Slow version - before ===
//
// Split on multiple characters using new char[] inline.
//
string t = "string to split, ok";
for (int i = 0; i < 10000000; i++)
{
string[] s = t.Split(new char[] { ' ', ',' });
}
=== Fast version - after ===
//
// Split on multiple characters using new char[] already created.
//
string t = "string to split, ok";
char[] c = new char[]{ ' ', ',' }; // <-- Cache this
for (int i = 0; i < 10000000; i++)
{
string[] s = t.Split(c);
}
Interpretation of the above table. We see that storing the array of delimiters separately is good. My measurements show the above code is less than 10% faster when the array is stored outside the loop.
C# has no explode method exactly like PHP explode, but you can gain the functionality quite easily with Split, for the most part. You can replace explode with the Split method that receives a string[] array. [C# PHP explode Function - dotnetperls.com]
Here we saw several examples and two benchmarks of the Split method in the C# programming language. You can use Split to divide or separate your strings while keeping your code as simple as possible. Sometimes, using IndexOf and Substring together to parse your strings can be more precise and less error-prone. [C# IndexOf String Examples - dotnetperls.com]
鍙兘杈撳叆鏁板瓧錛?^[0-9]*$"銆?br />
鍙兘杈撳叆n浣嶇殑鏁板瓧錛?^\d{n}$"銆?br />
鍙兘杈撳叆鑷沖皯n浣嶇殑鏁板瓧錛?^\d{n,}$"銆?br />
鍙兘杈撳叆m~n浣嶇殑鏁板瓧錛氥?^\d{m,n}$"
鍙兘杈撳叆闆跺拰闈為浂寮澶寸殑鏁板瓧錛?^(0|[1-9][0-9]*)$"銆?br />
鍙兘杈撳叆鏈変袱浣嶅皬鏁扮殑姝e疄鏁幫細"^[0-9]+(.[0-9]{2})?$"銆?br />
鍙兘杈撳叆鏈?~3浣嶅皬鏁扮殑姝e疄鏁幫細"^[0-9]+(.[0-9]{1,3})?$"銆?br />
鍙兘杈撳叆闈為浂鐨勬鏁存暟錛?^\+?[1-9][0-9]*$"銆?br />
鍙兘杈撳叆闈為浂鐨勮礋鏁存暟錛?^\-[1-9][]0-9"*$銆?br />
鍙兘杈撳叆闀垮害涓?鐨勫瓧絎︼細"^.{3}$"銆?br />
鍙兘杈撳叆鐢?6涓嫳鏂囧瓧姣嶇粍鎴愮殑瀛楃涓詫細"^[A-Za-z]+$"銆?br />
鍙兘杈撳叆鐢?6涓ぇ鍐欒嫳鏂囧瓧姣嶇粍鎴愮殑瀛楃涓詫細"^[A-Z]+$"銆?br />
鍙兘杈撳叆鐢?6涓皬鍐欒嫳鏂囧瓧姣嶇粍鎴愮殑瀛楃涓詫細"^[a-z]+$"銆?br />
鍙兘杈撳叆鐢辨暟瀛楀拰26涓嫳鏂囧瓧姣嶇粍鎴愮殑瀛楃涓詫細"^[A-Za-z0-9]+$"銆?br />
鍙兘杈撳叆鐢辨暟瀛椼?6涓嫳鏂囧瓧姣嶆垨鑰呬笅鍒掔嚎緇勬垚鐨勫瓧絎︿覆錛?^\w+$"銆?br />
楠岃瘉鐢ㄦ埛瀵嗙爜錛?^[a-zA-Z]\w{5,17}$"姝g‘鏍煎紡涓猴細浠ュ瓧姣嶅紑澶達紝闀垮害鍦?~18涔嬮棿錛屽彧鑳藉寘鍚瓧絎︺佹暟瀛楀拰涓嬪垝綰褲?br />
楠岃瘉鏄惁鍚湁^%&',;=?$\"絳夊瓧絎︼細"[^%&',;=?$\x22]+"銆?br />
鍙兘杈撳叆姹夊瓧錛?^[\u4e00-\u9fa5]{0,}$"
楠岃瘉Email鍦板潃錛?^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"銆?br />
楠岃瘉InternetURL錛?^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"銆?br />
楠岃瘉鐢佃瘽鍙風爜錛?^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"姝g‘鏍煎紡涓猴細"XXX-XXXXXXX"銆?XXXX-XXXXXXXX"銆?XXX-XXXXXXX"銆?XXX-XXXXXXXX"銆?XXXXXXX"鍜?XXXXXXXX"銆?br />
楠岃瘉韜喚璇佸彿錛?5浣嶆垨18浣嶆暟瀛楋級錛?^\d{15}|\d{18}$"銆?br />
楠岃瘉涓騫寸殑12涓湀錛?^(0?[1-9]|1[0-2])$"姝g‘鏍煎紡涓猴細"01"锝?09"鍜?1"锝?12"銆?br />
楠岃瘉涓涓湀鐨?1澶╋細"^((0?[1-9])|((1|2)[0-9])|30|31)$"姝g‘鏍煎紡涓猴紱"01"锝?09"鍜?1"锝?31"銆?
鍒╃敤姝e垯琛ㄨ揪寮忛檺鍒剁綉欏佃〃鍗曢噷鐨勬枃鏈杈撳叆鍐呭錛?/p>
鐢ㄦ鍒欒〃杈懼紡闄愬埗鍙兘杈撳叆涓枃錛歰nkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
鐢ㄦ鍒欒〃杈懼紡闄愬埗鍙兘杈撳叆鍏ㄨ瀛楃錛?onkeyup="value=value.replace(/[^\uFF00-\uFFFF] /g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
鐢ㄦ鍒欒〃杈懼紡闄愬埗鍙兘杈撳叆鏁板瓧錛歰nkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
鐢ㄦ鍒欒〃杈懼紡闄愬埗鍙兘杈撳叆鏁板瓧鍜岃嫳鏂囷細onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
寰楃敤姝e垯琛ㄨ揪寮忎粠URL鍦板潃涓彁鍙栨枃浠跺悕鐨刯avascript紼嬪簭錛屽涓嬬粨鏋滀負page1
s="http://www.9499.net/page1.htm"
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2")
alert(s)
鍖歸厤鍙屽瓧鑺傚瓧絎?鍖呮嫭姹夊瓧鍦ㄥ唴)錛歔^\x00-\xff]
搴旂敤錛氳綆楀瓧絎︿覆鐨勯暱搴︼紙涓涓弻瀛楄妭瀛楃闀垮害璁?錛孉SCII瀛楃璁?錛?/p>
String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}
鍖歸厤絀鴻鐨勬鍒欒〃杈懼紡錛歕n[\s| ]*\r
鍖歸厤HTML鏍囪鐨勬鍒欒〃杈懼紡錛?<(.*)>.*<\/\1>|<(.*) \/>/
鍖歸厤棣栧熬絀烘牸鐨勬鍒欒〃杈懼紡錛?^\s*)|(\s*$)
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
鍒╃敤姝e垯琛ㄨ揪寮忓垎瑙e拰杞崲IP鍦板潃錛?/p>
涓嬮潰鏄埄鐢ㄦ鍒欒〃杈懼紡鍖歸厤IP鍦板潃錛屽茍灝咺P鍦板潃杞崲鎴愬搴旀暟鍊肩殑Javascript紼嬪簭錛?/p>
function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //鍖歸厤IP鍦板潃鐨勬鍒欒〃杈懼紡
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}
涓嶈繃涓婇潰鐨勭▼搴忓鏋滀笉鐢ㄦ鍒欒〃杈懼紡錛岃岀洿鎺ョ敤split鍑芥暟鏉ュ垎瑙e彲鑳芥洿綆鍗曪紝紼嬪簭濡備笅錛?/p>
var ip="10.100.20.168"
ip=ip.split(".")
alert("IP鍊兼槸錛?+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
絎﹀彿瑙i噴錛?/p>
瀛楃
鎻忚堪
\
灝嗕笅涓涓瓧絎︽爣璁頒負涓涓壒孌婂瓧絎︺佹垨涓涓師涔夊瓧絎︺佹垨涓涓?鍚戝悗寮曠敤銆佹垨涓涓叓榪涘埗杞箟絎︺備緥濡傦紝'n' 鍖歸厤瀛楃 "n"銆?\n' 鍖歸厤涓涓崲琛岀銆傚簭鍒?'\\' 鍖歸厤 "\" 鑰?"\(" 鍒欏尮閰?"("銆?/p>
^
鍖歸厤杈撳叆瀛楃涓茬殑寮濮嬩綅緗傚鏋滆緗簡 RegExp 瀵硅薄鐨?Multiline 灞炴э紝^ 涔熷尮閰?'\n' 鎴?'\r' 涔嬪悗鐨勪綅緗?/p>
$
鍖歸厤杈撳叆瀛楃涓茬殑緇撴潫浣嶇疆銆傚鏋滆緗簡RegExp 瀵硅薄鐨?Multiline 灞炴э紝$ 涔熷尮閰?'\n' 鎴?'\r' 涔嬪墠鐨勪綅緗?/p>
*
鍖歸厤鍓嶉潰鐨勫瓙琛ㄨ揪寮忛浂嬈℃垨澶氭銆備緥濡傦紝zo* 鑳藉尮閰?"z" 浠ュ強 "zoo"銆? 絳変環浜巤0,}銆?/p>
+
鍖歸厤鍓嶉潰鐨勫瓙琛ㄨ揪寮忎竴嬈℃垨澶氭銆備緥濡傦紝'zo+' 鑳藉尮閰?"zo" 浠ュ強 "zoo"錛屼絾涓嶈兘鍖歸厤 "z"銆? 絳変環浜?{1,}銆?/p>
?
鍖歸厤鍓嶉潰鐨勫瓙琛ㄨ揪寮忛浂嬈℃垨涓嬈°備緥濡傦紝"do(es)?" 鍙互鍖歸厤 "do" 鎴?"does" 涓殑"do" 銆? 絳変環浜?{0,1}銆?/p>
{n}
n 鏄竴涓潪璐熸暣鏁般傚尮閰嶇‘瀹氱殑 n 嬈°備緥濡傦紝'o{2}' 涓嶈兘鍖歸厤 "Bob" 涓殑 'o'錛屼絾鏄兘鍖歸厤 "food" 涓殑涓や釜 o銆?/p>
{n,}
n 鏄竴涓潪璐熸暣鏁般傝嚦灝戝尮閰峮 嬈°備緥濡傦紝'o{2,}' 涓嶈兘鍖歸厤 "Bob" 涓殑 'o'錛屼絾鑳藉尮閰?"foooood" 涓殑鎵鏈?o銆?o{1,}' 絳変環浜?'o+'銆?o{0,}' 鍒欑瓑浠蜂簬 'o*'銆?/p>
{n,m}
m 鍜?n 鍧囦負闈炶礋鏁存暟錛屽叾涓璶 <= m銆傛渶灝戝尮閰?n 嬈′笖鏈澶氬尮閰?m 嬈°備緥濡傦紝"o{1,3}" 灝嗗尮閰?"fooooood" 涓殑鍓嶄笁涓?o銆?o{0,1}' 絳変環浜?'o?'銆傝娉ㄦ剰鍦ㄩ楀彿鍜屼袱涓暟涔嬮棿涓嶈兘鏈夌┖鏍箋?/p>
?
褰撹瀛楃绱ц窡鍦ㄤ換浣曚竴涓叾浠栭檺鍒剁 (*, +, ?, {n}, {n,}, {n,m}) 鍚庨潰鏃訛紝鍖歸厤妯″紡鏄潪璐┆鐨勩傞潪璐┆妯″紡灝藉彲鑳藉皯鐨勫尮閰嶆墍鎼滅儲鐨勫瓧絎︿覆錛岃岄粯璁ょ殑璐┆妯″紡鍒欏敖鍙兘澶氱殑鍖歸厤鎵鎼滅儲鐨勫瓧絎︿覆銆備緥濡傦紝瀵逛簬瀛楃涓?"oooo"錛?o+?' 灝嗗尮閰嶅崟涓?"o"錛岃?'o+' 灝嗗尮閰嶆墍鏈?'o'銆?/p>
.
鍖歸厤闄?"\n" 涔嬪鐨勪換浣曞崟涓瓧絎︺傝鍖歸厤鍖呮嫭 '\n' 鍦ㄥ唴鐨勪換浣曞瓧絎︼紝璇蜂嬌鐢ㄨ薄 '[.\n]' 鐨勬ā寮忋?/p>
(pattern)
鍖歸厤 pattern 騫惰幏鍙栬繖涓鍖歸厤銆傛墍鑾峰彇鐨勫尮閰嶅彲浠ヤ粠浜х敓鐨?Matches 闆嗗悎寰楀埌錛屽湪VBScript 涓嬌鐢?SubMatches 闆嗗悎錛屽湪JScript 涓垯浣跨敤 $0…$9 灞炴с傝鍖歸厤鍦嗘嫭鍙峰瓧絎︼紝璇蜂嬌鐢?'\(' 鎴?'\)'銆?/p>
(?:pattern)
鍖歸厤 pattern 浣嗕笉鑾峰彇鍖歸厤緇撴灉錛屼篃灝辨槸璇磋繖鏄竴涓潪鑾峰彇鍖歸厤錛屼笉榪涜瀛樺偍渚涗互鍚庝嬌鐢ㄣ傝繖鍦ㄤ嬌鐢?"鎴? 瀛楃 (|) 鏉ョ粍鍚堜竴涓ā寮忕殑鍚勪釜閮ㄥ垎鏄緢鏈夌敤銆備緥濡傦紝 'industr(?:y|ies) 灝辨槸涓涓瘮 'industry|industries' 鏇寸畝鐣ョ殑琛ㄨ揪寮忋?/p>
(?=pattern)
姝e悜棰勬煡錛屽湪浠諱綍鍖歸厤 pattern 鐨勫瓧絎︿覆寮濮嬪鍖歸厤鏌ユ壘瀛楃涓層傝繖鏄竴涓潪鑾峰彇鍖歸厤錛屼篃灝辨槸璇達紝璇ュ尮閰嶄笉闇瑕佽幏鍙栦緵浠ュ悗浣跨敤銆備緥濡傦紝'Windows (?=95|98|NT|2000)' 鑳藉尮閰?"Windows 2000" 涓殑 "Windows" 錛屼絾涓嶈兘鍖歸厤 "Windows 3.1" 涓殑 "Windows"銆傞鏌ヤ笉娑堣楀瓧絎︼紝涔熷氨鏄錛屽湪涓涓尮閰嶅彂鐢熷悗錛屽湪鏈鍚庝竴嬈″尮閰嶄箣鍚庣珛鍗沖紑濮嬩笅涓嬈″尮閰嶇殑鎼滅儲錛岃屼笉鏄粠鍖呭惈棰勬煡鐨勫瓧絎︿箣鍚庡紑濮嬨?/p>
(?!pattern)
璐熷悜棰勬煡錛屽湪浠諱綍涓嶅尮閰?pattern 鐨勫瓧絎︿覆寮濮嬪鍖歸厤鏌ユ壘瀛楃涓層傝繖鏄竴涓潪鑾峰彇鍖歸厤錛屼篃灝辨槸璇達紝璇ュ尮閰嶄笉闇瑕佽幏鍙栦緵浠ュ悗浣跨敤銆備緥濡?Windows (?!95|98|NT|2000)' 鑳藉尮閰?"Windows 3.1" 涓殑 "Windows"錛屼絾涓嶈兘鍖歸厤 "Windows 2000" 涓殑 "Windows"銆傞鏌ヤ笉娑堣楀瓧絎︼紝涔熷氨鏄錛屽湪涓涓尮閰嶅彂鐢熷悗錛屽湪鏈鍚庝竴嬈″尮閰嶄箣鍚庣珛鍗沖紑濮嬩笅涓嬈″尮閰嶇殑鎼滅儲錛岃屼笉鏄粠鍖呭惈棰勬煡鐨勫瓧絎︿箣鍚庡紑濮?/p>
x|y
鍖歸厤 x 鎴?y銆備緥濡傦紝'z|food' 鑳藉尮閰?"z" 鎴?"food"銆?(z|f)ood' 鍒欏尮閰?"zood" 鎴?"food"銆?/p>
[xyz]
瀛楃闆嗗悎銆傚尮閰嶆墍鍖呭惈鐨勪換鎰忎竴涓瓧絎︺備緥濡傦紝 '[abc]' 鍙互鍖歸厤 "plain" 涓殑 'a'銆?/p>
[^xyz]
璐熷煎瓧絎﹂泦鍚堛傚尮閰嶆湭鍖呭惈鐨勪換鎰忓瓧絎︺備緥濡傦紝 '[^abc]' 鍙互鍖歸厤 "plain" 涓殑'p'銆?/p>
[a-z]
瀛楃鑼冨洿銆傚尮閰嶆寚瀹氳寖鍥村唴鐨勪換鎰忓瓧絎︺備緥濡傦紝'[a-z]' 鍙互鍖歸厤 'a' 鍒?'z' 鑼冨洿鍐呯殑浠繪剰灝忓啓瀛楁瘝瀛楃銆?/p>
[^a-z]
璐熷煎瓧絎﹁寖鍥淬傚尮閰嶄換浣曚笉鍦ㄦ寚瀹氳寖鍥村唴鐨勪換鎰忓瓧絎︺備緥濡傦紝'[^a-z]' 鍙互鍖歸厤浠諱綍涓嶅湪 'a' 鍒?'z' 鑼冨洿鍐呯殑浠繪剰瀛楃銆?/p>
\b
鍖歸厤涓涓崟璇嶈竟鐣岋紝涔熷氨鏄寚鍗曡瘝鍜岀┖鏍奸棿鐨勪綅緗備緥濡傦紝 'er\b' 鍙互鍖歸厤"never" 涓殑 'er'錛屼絾涓嶈兘鍖歸厤 "verb" 涓殑 'er'銆?/p>
\B
鍖歸厤闈炲崟璇嶈竟鐣屻?er\B' 鑳藉尮閰?"verb" 涓殑 'er'錛屼絾涓嶈兘鍖歸厤 "never" 涓殑 'er'銆?/p>
\cx
鍖歸厤鐢?x 鎸囨槑鐨勬帶鍒跺瓧絎︺備緥濡傦紝 \cM 鍖歸厤涓涓?Control-M 鎴栧洖杞︾銆倄 鐨勫煎繀欏諱負 A-Z 鎴?a-z 涔嬩竴銆傚惁鍒欙紝灝?c 瑙嗕負涓涓師涔夌殑 'c' 瀛楃銆?/p>
\d
鍖歸厤涓涓暟瀛楀瓧絎︺傜瓑浠蜂簬 [0-9]銆?/p>
\D
鍖歸厤涓涓潪鏁板瓧瀛楃銆傜瓑浠蜂簬 [^0-9]銆?/p>
\f
鍖歸厤涓涓崲欏電銆傜瓑浠蜂簬 \x0c 鍜?\cL銆?/p>
\n
鍖歸厤涓涓崲琛岀銆傜瓑浠蜂簬 \x0a 鍜?\cJ銆?/p>
\r
鍖歸厤涓涓洖杞︾銆傜瓑浠蜂簬 \x0d 鍜?\cM銆?/p>
\s
鍖歸厤浠諱綍絀虹櫧瀛楃錛屽寘鎷┖鏍箋佸埗琛ㄧ銆佹崲欏電絳夌瓑銆傜瓑浠蜂簬 [ \f\n\r\t\v]銆?/p>
\S
鍖歸厤浠諱綍闈炵┖鐧藉瓧絎︺傜瓑浠蜂簬 [^ \f\n\r\t\v]銆?/p>
\t
鍖歸厤涓涓埗琛ㄧ銆傜瓑浠蜂簬 \x09 鍜?\cI銆?/p>
\v
鍖歸厤涓涓瀭鐩村埗琛ㄧ銆傜瓑浠蜂簬 \x0b 鍜?\cK銆?/p>
\w
鍖歸厤鍖呮嫭涓嬪垝綰跨殑浠諱綍鍗曡瘝瀛楃銆傜瓑浠蜂簬'[A-Za-z0-9_]'銆?/p>
\W
鍖歸厤浠諱綍闈炲崟璇嶅瓧絎︺傜瓑浠蜂簬 '[^A-Za-z0-9_]'銆?/p>
\xn
鍖歸厤 n錛屽叾涓?n 涓哄崄鍏繘鍒惰漿涔夊箋傚崄鍏繘鍒惰漿涔夊煎繀欏諱負紜畾鐨勪袱涓暟瀛楅暱銆備緥濡傦紝'\x41' 鍖歸厤 "A"銆?\x041' 鍒欑瓑浠蜂簬 '\x04' & "1"銆傛鍒欒〃杈懼紡涓彲浠ヤ嬌鐢?ASCII 緙栫爜銆?
\num
鍖歸厤 num錛屽叾涓?num 鏄竴涓鏁存暟銆傚鎵鑾峰彇鐨勫尮閰嶇殑寮曠敤銆備緥濡傦紝'(.)\1' 鍖歸厤涓や釜榪炵畫鐨勭浉鍚屽瓧絎︺?/p>
\n
鏍囪瘑涓涓叓榪涘埗杞箟鍊兼垨涓涓悜鍚庡紩鐢ㄣ傚鏋?\n 涔嬪墠鑷沖皯 n 涓幏鍙栫殑瀛愯〃杈懼紡錛屽垯 n 涓哄悜鍚庡紩鐢ㄣ傚惁鍒欙紝濡傛灉 n 涓哄叓榪涘埗鏁板瓧 (0-7)錛屽垯 n 涓轟竴涓叓榪涘埗杞箟鍊箋?/p>
\nm
鏍囪瘑涓涓叓榪涘埗杞箟鍊兼垨涓涓悜鍚庡紩鐢ㄣ傚鏋?\nm 涔嬪墠鑷沖皯鏈?nm 涓幏寰楀瓙琛ㄨ揪寮忥紝鍒?nm 涓哄悜鍚庡紩鐢ㄣ傚鏋?\nm 涔嬪墠鑷沖皯鏈?n 涓幏鍙栵紝鍒?n 涓轟竴涓悗璺熸枃瀛?m 鐨勫悜鍚庡紩鐢ㄣ傚鏋滃墠闈㈢殑鏉′歡閮戒笉婊¤凍錛岃嫢 n 鍜?m 鍧囦負鍏繘鍒舵暟瀛?(0-7)錛屽垯 \nm 灝嗗尮閰嶅叓榪涘埗杞箟鍊?nm銆?/p>
\nml
濡傛灉 n 涓哄叓榪涘埗鏁板瓧 (0-3)錛屼笖 m 鍜?l 鍧囦負鍏繘鍒舵暟瀛?(0-7)錛屽垯鍖歸厤鍏繘鍒惰漿涔夊?nml銆?/p>
\un
鍖歸厤 n錛屽叾涓?n 鏄竴涓敤鍥涗釜鍗佸叚榪涘埗鏁板瓧琛ㄧず鐨?Unicode 瀛楃銆備緥濡傦紝 \u00A9 鍖歸厤鐗堟潈絎﹀彿 (?)銆?/p>