English 中文(简体)
Java 非常简单的经常表达方式
原标题:Java very simple regular expression puzzle
  • 时间:2011-10-31 04:06:29
  •  标签:
  • java
  • regex

我理解 Java的定期表述可从<代码>String smatches益准方法上查阅,也可从长途经上传到<代码>Pattern等。 因此,以下代码应实际印成“Y>项目”。 它印刷了“Y”、“Y”、“无”线。 我失踪了什么?

import java.util.regex.*;

public class TestRegex {
public static void main(String[] args) {
    String pattern = "html";
    String input   = "somehtml.txt";

    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(input);

    if(m.find()) {
        System.out.println("Yes!");
    }
    else {
        System.out.println("no");
    }

    if(input.matches(pattern)) {
        System.out.println("Yes!");
    }
    else {
        System.out.println("no");
    }

}

}

产出:

Yes!
no

Java edition 1.6 on Win7 64-bit.

C:UsersMichael Smith>java -version java version "1.6.0_24" Java(TM) SE Runtime Environment (build 1.6.0_24-b07) Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

最佳回答

http://www.un.org/Depts/DGACM/index_french.htmfind ( only look for a line some where in the string.

问题回答

String#Matches method checks the pattern against the entire input text. This is the reason your code prints no second time. Matcher#Find scans input to find next matching subsequence in the input.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签