首页 > Java > java教程 > 正文

Java模式类

WBOY
发布: 2024-08-30 16:00:50
原创
269 人浏览过

Java 模式类是 java.util 包中提供的公共最终类,用于创建正则表达式的编译表示。所有正则表达式都会转换为此类的实例。它们用于创建 Matcher 类的实例,该类由 matches 方法组成,该方法比较以获取与正则表达式匹配的字符串,如果字符串匹配则返回 true,否则返回 false。该类的实例是不可变的并且是线程安全的,这意味着它的实例对于并发工作的不同线程的工作没有影响。

Java 模式类方法

下面给出的是java模式类方法:

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

1.公共静态模式编译(字符串myregex)

此方法有助于根据给定的正则表达式创建模式。

Parameters The regular expression to be compiled in the form of string.

Example: ( .*) ( \hh) ( .*)

参数

要编译为字符串形式的正则表达式。 示例:
    ( .*) ( \hh) ( .*)
表>
  • 例外:

    当传递给方法的表达式的语法无效时,会抛出PatternSyntaxException。

    示例:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String MYREGEX =  " ( .*) (Learn) ( .*)?";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (MYREGEX);
    System.out.println ( "Regular Expression " +pattern.pattern ());
    }
    }
    登录后复制

    代码:

    Java模式类

    输出:

    Parameters

     

    a. myregex: The regular expression to be compiled in the form of string.
    b. flag: Flags are the specifications that need to be provided while compiling a regular expression. Example are CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE.

    2.公共静态模式编译(字符串myregex,int标志) 此方法有助于使用正则表达式和指定标志创建 Pattern 类的实例。

      参数
    •  
    a。

    myregex:要编译的字符串形式的正则表达式。

    b。

    flag: 标志是编译正则表达式时需要提供的规范。例如 CASE_INSENSITIVE、MULTILINE、DOTALL、UNICODE_CASE。

    表> 例外:
    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String REGEX =  " ( .*) (Learn) ( .*)?";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (REGEX, Pattern.CASE_INSENSITIVE);
    System.out.println ( "Regular Expression " +pattern.pattern ());
    }
    }
    登录后复制

    当传递给方法的表达式的语法无效时,会抛出PatternSyntaxException。

    如果位值与传递的标志匹配的值不同,则会抛出 IllegalArgumentException。Java模式类

    示例:

    代码:

    输出:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String REGEX =  " ( .*) (Learn) ( .*)?";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (REGEX, Pattern.UNICODE_CHARACTER_CLASS );
    System.out.println ( "Flag for UNICODE_CHARACTER_CLASS " +pattern.flags ());
    Pattern pattern2= Pattern.compile (REGEX, Pattern.UNICODE_CASE );
    System.out.println ( "Flag for UNICODE_CASE " +pattern2.flags ());
    }
    }
    登录后复制

    3.公共 int 标志 ()

    Java模式类此方法有助于获取在编译正则表达式以创建模式类实例时设置的标志的整数值。

    示例:

    Parameters inputSequence: It is a sequence of character to which we need to match the compiled instance of regex in the form of a pattern object.

    代码:

    输出:

    import java.util.regex.MatchResult;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class HelloWorld {
    private static final String MYREG=  " ( .*) (gu)";
    private static final String SEQ ="Learning regular expression helps a lot in Artificial Intelligence";
    public static void main (String[] args) {
    Pattern pattern = Pattern.compile (MYREG, Pattern.UNICODE_CHARACTER_CLASS );
    Matcher matcherObj = pattern.matcher (SEQ);
    if (matcherObj.find ()) {
    MatchResult res = matcherObj.toMatchResult ();
    System.out.println ( "The offset  : "+res.end ());
    }
    }
    }
    登录后复制

    4.公共 Matcher 匹配器 (CharSequenceinputSequence)

    此方法用于通过将正则表达式与给定的输入序列进行比较来创建匹配器对象的实例。该方法需要传递1个参数,不会抛出异常。Java模式类

    参数

    inputSequence:

    它是一个字符序列,我们需要将模式对象形式的正则表达式的已编译实例与之匹配。

    表>
    Parameters inputSequence: It is a sequence of character to which we need to match the compiled instance of regex in the form of a pattern object.
    MyRegex: The regular expression to be compiled in the form of string.
    示例: 代码: 输出: 5. public static booleanmatches (String MyRegex, CharSequenceinputSequence) 此方法用于将给定的输入序列与正则表达式的编译版本进行匹配。 参数 inputSequence: 它是一个字符序列,我们需要将模式对象形式的正则表达式的已编译实例与之匹配。 MyRegex: 要编译的字符串形式的正则表达式。 表>

    Exception:

    • PatternSyntaxException is thrown when the syntax of the expression is invalid being passed to the method.

    6. public String pattern ()

    This method is used to get the regular expression from which the pattern instance has been compiled. This method requires no parameters to be passed as well as no exception will be thrown.

    An example for this method is shown below with an example of the quote method.

    7. public static String quote (String s)

    This method is used to get the literal string after the pattern is resolved using the method’s flags. This method requires 1 parameter to be passed as well as no exception will be thrown.

    Parameters The string of regular expression that needs to be compiled.

    Example:

    Code:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String myRegx = "location$";
    private static String line= "The location$ office have great capacity of seats for our employess";
    private static String newEx = "Bangalore";
    public static void main (String[] args) {
    Pattern patternObj2 = Pattern.compile (Pattern.quote (myRegx));
    Matcher matcherObj1 = patternObj2.matcher (line);
    line= matcherObj1.replaceAll (newEx);
    System.out.println ( "The Regular expression defined for pattern instance is " +patternObj2.pattern ());
    System.out.println ( "The Input sequence after substitution of Regular Expression  "+ line);
    }
    }
    登录后复制

    Output:

    Java模式类

    8. public String[] split (CharaterSequence in)

    This method is used to split the input String argument at every index where regular expression is found. This method requires 1 parameter to be passed as well as no exception will be thrown.

    Parameters  input: The character sequence that is to be split according to the Regular expression specified.

    Example:

    Code:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String REGEX = ":";
    private static String INPUT = "BANGLORE:CHENNAI:GUWAHATI:AHMEDABAD";
    public static void main (String[] args) {
    Pattern patternObj2 = Pattern.compile (REGEX);
    String[] result = patternObj2.split (INPUT);
    for (String data:result){
    System.out.println (data+"\n");
    }
    }
    }
    登录后复制

    Output:

    Java模式类

    9. public String[] split (CharaterSequencein, intmyThreshHold)

    This method also splits the given input sequence but upto some threshold. This method requires 2 parameters to be passed as well as no exception will be thrown.

    Parameters a. in: The character sequence that is to be split.
    b. myThreshHold: This parameter defines the threshold of splitting operation.

    Example:

    Code:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String REGEX = ":";
    private static String INPUT = "BANGLORE:CHENNAI:GUWAHATI:AHMEDABAD";
    public static void main (String[] args) {
    Pattern patternObj2 = Pattern.compile (REGEX);
    String[] result = patternObj2.split (INPUT,3);
    for (String data:result){
    System.out.println (data+"\n");
    }
    }
    }
    登录后复制

    Output:

    Java模式类

    10. public String toString ()

    This method is used to get the String representation of the regular expression from which the regular expression has been compiled. This method requires no parameters to be passed as well as no exception will be thrown.

    Example:

    Code:

    import java.util.regex.Pattern;
    public class HelloWorld {
    private static String REGEX = "location$";
    public static void main (String[] args) {
    Pattern patternObj2 =
    Pattern.compile (Pattern.quote (REGEX));
    System.out.println ( "toString representation of pattern instance is " +patternObj2.toString ());
    }
    }
    登录后复制

    Output:

    Java模式类

    Conclusion

    Pattern class is used to store the compiled representation of regular expressions from where they are passed to an instance of Matcher class to get the strings that match that regular expression. In this way, one is able to work with regular expressions more efficiently in their application.

    以上是Java模式类的详细内容。更多信息请关注PHP中文网其他相关文章!

  • 相关标签:
    来源:php
    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板