逃避正则表达式

时间:2015-10-25 19:11:36

标签: ios regex swift

我有一个带正则表达式的xml文件。当我阅读它时,我在一个名为News.objects.all()的变量中得到一个表达式<a class="call-to-action-details".*?\shref="(.*?)"

然后我调用我的字符串方法:regex并且它什么都不返回。

但是如果我调用我的正则表达式方法并转义某些字符,那么它会返回结果:

print(text.searchRegex(regex))

如何逃避角色?或者我做错了什么?我的regexSearch功能:

print(text.searchRegex("<a class=\"call-to-action-details\".*?\\shref=\"(.*?)\""))

1 个答案:

答案 0 :(得分:0)

在字符串文字中,反斜杠( Console.ReadLine() Console.Clear() Console.Title = ("Desktop TBI | Hello " + username) Console.WriteLine("-----------------------------------------------------------------------------------------------------------") Console.WriteLine("Please select an option, note that this is a work in progress, and will contain more features in the future") Console.WriteLine("") Console.WriteLine(" Type 'File' to create the directory (Important if you want to add plugins)") Console.WriteLine(" Using Version: " + VersionName) Console.WriteLine("-----------------------------------------------------------------------------------------------------------") Console.WriteLine(" Please choose what action you want to do") Console.WriteLine(" Type in 'File' To find the directory") Console.WriteLine(" Type in 'Update' To open the .bat file to download the updates") Console.WriteLine("-----------------------------------------------------------------------------------------------------------") Console.WriteLine("To create the new path, enter 'CreateDir'") REM Begin the part where users can select their code. Dim selection As String = Console.ReadLine Select Case selection REM This allows the creation of a text file. Case "File" Console.Clear() File() REM Private sub selection REM Updates their program. Case "Username" Console.Clear() GoTo UserName REM Set's their username for the program. Case "Update" Update() Case "KeyList" KeyList() Case "CreateDir" CreateDir() Case "SERV_Start" Chat_Start_SERV() Console.ReadLine() Case "PressAKey" PressAkey() End Select End Sub Private Sub File() Dim FILE_NAME As String = "C:\Desktop\Test.txt" Dim i As Integer Dim aryText(4) As String aryText(0) = "Mary WriteLine" aryText(1) = "Had" aryText(2) = "Another" aryText(3) = "Little" aryText(4) = "One" Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) For i = 0 To 4 objWriter.WriteLine(aryText(i)) Next objWriter.Close() MsgBox("Your file has been loaded.") End Sub 'State the version name Dim version As String = "1" Dim ICollection As String = Console.ReadLine Private Sub Update() If version Then Console.WriteLine("It appears you are using 1.0 version, do you want to scan for updates?") Console.WriteLine("Please note, this will take you to a .batch file. Do not edit any of the content") Console.WriteLine("As it could potentially mess up the file") Select Case ICollection Case "Yes" GoTo site Case "No" End Select site: Process.Start("C:\VBTBI\Programs\Update.bat") End If End Sub )和双引号(\)需要使用反斜杠进行转义。

对于您的模式字符串,使用"的相同名称作为结果regex可能也是一个坏主意。

从文件中读取正则表达式时,您不需要像文字一样转义字符。

的表达式
NSRegularExpression
从文件中读入时,{p>可以直接使用

如果您没有与文件中的正则表达式匹配,请尝试检查正在运行的正则表达式文字与您正在读取的字符串是否相等。某处可能存在一个额外的字符,导致它们不匹配。< / p>

除此之外,您的代码将毫无问题地抓住匹配。