A little filtering of Java Regular Expressions (Java Regular Expressions determine IP legality)

Source: Internet
Author: User

I will not sell it to anyone here. in fact, almost all JavaScript regular expressions can be judged in Java. Of course, here we talk about written statements (not special ). it's just a double slash processing, and some class judgments are added. Java is a little more complicated than JavaScript. is to use the reference classes and methods in practice. well, let's talk about some of your small applications. here is an example of determining the IP address in practice.

 

There are quite a few regular expressions in Javascript writing. Since I made a small jxl Excel import control yesterday, I didn't consider its format judgment at the time, so I began to refine it today, maybe you are very familiar with some JavaScript regular expressions, and may be confused for several seconds when you go to Java. the same is true for me. I just switched to Java to determine whether the IP address is valid. at this time, I am sleepy. Because this is the first time I write Java regular expressions, I used to make a general judgment. I checked it online and added my own thoughts. (normal people's thinking. it turns out that a little bit of content can be used to solve the problem.

  1. Import java. util. RegEx .*;
  2. // Specific classes. You can check the API or its source file by yourself. I will not talk about it here. I am also a dish.
  3. // One bird

Well, with the package here, the others are actually used. First, we will provide a very simple method we have written. It is quite simple.

  1. // Test whether the IP address is valid
  2. Public Boolean isip (string IPaddress ){
  3. String test = "([1-9] | [1-9] // d | 1 // d {2} | 2 [0-1] // d | 22 [0-3]) (//. (// d | [1-9] // d | 1 // d {2} | 2 [0-4] // d | 25 [0-5]) {3 }";
  4. Pattern pattern = pattern. Compile (test );
  5. Matcher = pattern. matcher (IPaddress );
  6. Return matcher. Matches ();
  7. }

In a simple method, the test is actually written in Javascript, but a slash "/" is added when it is taken, which is so simple. Here is simply to say, in fact, it is not that complicated to use. I have found a lot on the Internet, but it is not satisfactory. Why? It is actually my own reason, because when I came into contact with new things, I always think there is something missing. in fact, this thing enables you to implement your functions and meet your requirements. However, it takes a while for you to use it. This is a process.

 

Well, here we are. I hope you can solve your problems and be brave enough to solve your own problems.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.