如何使用正则表达式模式匹配

时间:2016-08-30 13:25:20

标签: java regex

在SQL中,我们可以使用like关键字进行搜索。 前,

1. Pattern
2. Putty

可以使用查询like 'P_tt%'

检索这些字词

我如何在java中执行相同的过程。

2 个答案:

答案 0 :(得分:1)

您可以使用java.util.regex.Pattern。 例如:

 Pattern p = Pattern.compile("P_tt\\.*");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();

在你的情况下,它将是:

Dim wshShell, FSO, strDexcomFolder, strDexcom, SysRoot, intRunError, strGroup, strDomain, InstallLog

Const ForWriting = 2
Set InstallLog = FSO.OpenTextFile("Install_Log.txt", ForWriting)

Set wshShell = CreateObject("WScript.Shell")
SysRoot = WshShell.ExpandEnvironmentStrings("%SystemDrive%")
Set FSO = CreateObject("Scripting.FileSystemObject")
strDexcomFolder = "c:\Program Files (x86)\Bioex"
strDomain = "xxxxxxxx"
strGroup = "domain users"

msgbox strDexcomFolder
If FSO.FolderExists(strDexcomFolder) Then
msgbox"start"
intRunError = WshShell.Run("icacls """ & strDexcomFolder & """ /grant " & strDomain & "\" & strGroup & ":(OI)(CI)(M) ", 2, True)
msgbox intRunError
   If Err.number <> 0 Then
    InstallLog.WriteLine("")
    InstallLog.WriteLine("Error Assigning Permissions!")
    InstallLog.WriteLine("Error #: "&Err.Number&", "&Err.Description&"") 
    InstallLog.WriteLine("")
    MsgBox"Error assigning permissions!"
    InstallLog.close
    End If
Else
    Wscript.Echo "Error: folder " & strDexcomFolder & " does not exist"
End If
WScript.Quit

有关更多访问Java文档: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

答案 1 :(得分:0)

你可能会这样:

String str1 = "MyString lalaa";
String str2 = "P_tt other string lala";
String pattern = "P_tt\\.*";
boolean isInStr1 = str1.matches(pattern); // return false
boolean isInStr2 = str2.matches(pattern); // return true

但请记住,LIKE无法通过正则表达式模式进行搜索。在Java中,您可以使用简单的正则表达式模式。在我的例子中,我建议&#34; P_tt\.*&#34;,这意味着&#34; P_tt&#34; - 你的常量词和&#34; \.&#34; - 任何字符(包括空格,制表符和其他字符),*是平均量迭代。 * - 零或更多。