如何在java中使用正则表达式(How do I use regular expressions in Java).docVIP

  • 2
  • 0
  • 约9.34千字
  • 约 14页
  • 2017-10-06 发布于河南
  • 举报

如何在java中使用正则表达式(How do I use regular expressions in Java).doc

如何在java中使用正则表达式(How do I use regular expressions in Java)

如何在java中使用正则表达式(How do I use regular expressions in Java) How do I use regular expressions in Java to handle text data? Release time: 2009.02.24 08:54 source: nokiaguy blog author: nokiaguy [-IT technology reported] regular expression is a string, but unlike ordinary strings, regular expressions are abstractions of a set of similar strings, such as the following strings: A98b, c0912d, c10b, , ab We carefully analyze the above five strings, we can see that they have a common characteristic, is the first character must beaorc, the last character must bebord, and in the middle of the character is any number of digits (including 0 digits). Therefore, we can abstract the common features of these five strings, which leads to a regular expression: [ac]\\d*[bd]. And according to this regular expression, we can write infinite strings that satisfy the condition. There are many ways to use regular expressions in Java, and the simplest is to use them together with strings. In String, there are four ways you can use regular expressions; they are matches, split, replaceAll, and replaceFirst. One, matches method The matches method can determine whether the current string matches a given regular expression. If match, returns true, otherwise returns false. The matches method is defined as follows: Public Boolean matches (String regex) As for the regular expressions given above, we can verify it with the following program. String[], SS = new, String[]{, a98b, c0912d, c10b, , ab}; For (String, s:, SS) System.out.println (s.matches ([ac]\\d*[bd])); Output result: True True True True True Heres a brief explanation of the meaning of this regular expression. If we have learned the lexical analysis of the compiler principle, it is easy to understand the regular expressions above (because the expressions of regular expressions are similar to those in lexical analysis). As in [...] the equivalent or |, such as the [abcd] equivalent of a|b|c|d, which is a or B or C or D. I

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档