找到 34423 篇文章 相关的编程

如何在 Java 正则表达式中找到与所需模式匹配的输入序列中的子序列?

Fendadis John
更新于 2019-07-30 22:30:24

411 次查看

find() 方法查找与所需模式匹配的输入序列中的子序列。此方法在 Matcher 类中可用,该类在 java.util.regex 包中可用。以下给出了一个演示 Java 正则表达式中 Matcher.find() 方法的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("Sun");       Matcher m = p.matcher("The Earth revolves around the Sun");       System.out.println("子序列: Sun");       System.out.println("序列: The Earth revolves around the Sun");       if (m.find())     ... 阅读更多

在 Java 正则表达式中使用 Matcher.end() 方法

Rishi Raj
更新于 2019-07-30 22:30:24

114 次查看

java.util.regex.Matcher.end() 方法返回根据正则表达式从序列中匹配到的最后一个字符之后的偏移值。此方法不需要参数。如果未发生匹配或匹配操作失败,则会抛出 IllegalStateException。以下给出了一个演示 Java 正则表达式中 Matcher.end() 方法的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("(a*b)");       Matcher m = p.matcher("caaabccaab");       System.out.println("输入字符串为: caaabccaab");       System.out.println("正则表达式为: (a*b)");   ... 阅读更多

在 Java 正则表达式中使用字符类

Vikyath Ram
更新于 2019-07-30 22:30:24

122 次查看

字符类是一组包含在方括号内的字符。字符类中的字符指定可以与输入字符串中的字符匹配以获得成功的字符。字符类的示例为 [a-z],它表示字母 a 到 z。以下给出了一个演示 Java 正则表达式中字符类的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("[a-z]+");       Matcher m = p.matcher("the sky is blue");       System.out.println("输入 ... 阅读更多

在 Java 正则表达式中使用 ? 量词

Arushi
更新于 2019-07-30 22:30:24

154 次查看

通常,? 量词表示指定模式的 0 次或 1 次出现。例如 - X? 表示 X 的 0 次或 1 次出现。正则表达式 "t.+?m" 用于使用 ? 量词在字符串 "tom and tim are best friends" 中查找匹配项。以下给出了一个演示此内容的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("t.+?m");       Matcher m = p.matcher("tom and tim are best friends");       System.out.println("输入字符串为: tom and tim are best ... 阅读更多

使用 find() 在 Java 正则表达式中查找子序列

Fendadis John
更新于 2019-07-30 22:30:24

60 次查看

find() 方法查找与所需模式匹配的输入序列中的子序列。此方法在 Matcher 类中可用,该类在 java.util.regex 包中可用以下给出了一个使用 find() 方法在 Java 中查找子序列的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       Pattern p = Pattern.compile("cool");       Matcher m = p.matcher("Java is cool");       System.out.println("子序列: cool");       System.out.println("序列: Java is cool");       if (m.find())          System.out.println("找到子序列"); ... 阅读更多

Java 正则表达式中的勉强量词

Rishi Raj
更新于 2019-07-30 22:30:24

135 次查看

勉强量词从尽可能短的字符串大小开始。如果引擎找到匹配项,则过程继续查找更多匹配项,否则引擎会向搜索字符串部分添加一个字符并再次尝试。这将持续进行,直到获得匹配项或使用完字符串为止。正则表达式 "B+?" 用于在字符串 "SkyIsBlue" 中查找匹配项。以下给出了一个演示此内容的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String regex = "B+?";       String str ... 阅读更多

Java 中的 extends 关键字

Vikyath Ram
更新于 2019-07-30 22:30:24

4K+ 次查看

对象可以使用继承获取另一个对象的属性和行为。在 Java 中,extends 关键字用于指示新类是使用继承从基类派生的。因此,基本上,extends 关键字用于扩展类的功能。以下给出了一个演示 Java 中 extends 关键字的程序:示例 实时演示class A {    int a = 9; } class B extends A {    int b = 4; } public class Demo {    public static void main(String args[]) {       B obj = new B();     ... 阅读更多

null 在 Java 中是文字吗?

Arushi
更新于 2019-07-30 22:30:24

734 次查看

Java 中的 null 是 null 类型的文字。它不能转换为基本类型,例如 int、float 等,但可以转换为引用类型。此外,null 不一定具有值 0。以下给出了一个演示 Java 中 null 的程序:示例 实时演示public class Demo {    public static void main(String args[]) {       String str = null;       System.out.println("str 的值为: " + str);    } }输出str 的值为: null现在让我们了解上述程序。在 main() 方法中,String str 被初始化 ... 阅读更多

Java 正则表达式中的贪婪量词

Fendadis John
更新于 2019-07-30 22:30:24

195 次查看

贪婪量词会尽可能多次重复指定的标记,然后引擎回溯,贪婪量词放弃匹配以最终找到所需的匹配项。正则表达式 "(\w+)(\d)(\w+)" 用于在字符串 "EarthHas1Moon" 中查找匹配项。以下给出了一个演示此内容的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) {       String str = "EarthHas1Moon";       String regex = "(\w+)(\d)(\w+)";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(str);       m.find();       ... 阅读更多

在 Java 中查找所有以 'a' 开头的单词

Rishi Raj
更新于 2019-07-30 22:30:24

540 次查看

可以使用 Java 中的正则表达式查找以字母 a 开头的所有单词。正则表达式是字符序列,可以使用特定的模式语法与其他字符串匹配。正则表达式在 java.util.regex 包中可用,该包包含许多类,但最重要的类是 Pattern 类和 Matcher 类。如下所示,一个使用正则表达式查找所有以 'a' 开头的单词的程序:示例 实时演示import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String args[]) throws Exception {       String ... 阅读更多

广告

© . All rights reserved.