0

Lets say you have a string like this:

198<string>12<string>88<string>201

Yes that looks like an IPv4 address, because it is one.

How do i check to see if there are repeated patterns in a string? I've no idea where to start, and im not sure that regex would help since i dont really know what string to look for, just that it will be repeated.

The goal here is to strip <string> from the main string.

Okay lets say the string is:

String test = "112(dot)115(dot)48(dot)582";
if(isIP(test){
   System.out.println("Yep, it's an ip");
}

The output should be:

Yep, it's an ip

The seperator (dot) will always be different.

5
  • Could you give some example? Commented Dec 7, 2014 at 21:47
  • Provide some valid input and expected output? Commented Dec 7, 2014 at 21:49
  • possible duplicate of Validating IPv4 string in Java Commented Dec 7, 2014 at 21:54
  • @ThatGuy343 Is "112(color)115(color)48(colour)582"; valid? Commented Dec 7, 2014 at 22:03
  • no, the separators being the same is fine, atleast for the sake of simplicity. Commented Dec 7, 2014 at 22:06

4 Answers 4

1

Try this: https://regex101.com/r/oR1gS8/4

/^((?:\d{1,3}[^0-9]+){3}\d{1,3})$/

Matches 198<string>12<string>88<string>201, 112(dot)115(dot)48(dot)582, and 112<test>115<test>48<test>582, among others...

Sign up to request clarification or add additional context in comments.

5 Comments

it will check correct ip in this example 11223472938(dot)115(dot)48(dot)582 but this isn't valid.
192.1.1 is Invalid IP4 but your regex returns true for it
Fixed... Take a look at the original post.
It will say 12x23.34-45 as valid.
Is it not? @ThatGuy343 implied the delimiter could be any string. The one weakness this regex has is that it'll allow groups with values above 255, which can be fixed if necessary.
1
/((((\d{1,3})\D{1,5}){3})(\d{1,3}))$/

112(dot)115(dot)48(dot)582

Matches

1.  [0-26]  `112(dot)115(dot)48(dot)582`
2.  [0-23]  `112(dot)115(dot)48(dot)`
3.  [16-23] `48(dot)`
4.  [16-18] `48`
5.  [23-26] `582`

control one by one your cases in here

8 Comments

interesting, im going to test this a bit more
It doesn't work for this: 112<test>115<test>48<test>582 but it works for this 112dot115dot48dot582
this also only works for dot, the ip separator could be any string, not just "dot".
All I know seperator should be certain value. It can be cause issues.
Added regex101 url, you can check your cases one by one.
|
0

You will need to capture group to accomplish this and use Non number D.

public boolean isIP(String test) {
    String regex = "\\d+(\\D+)\\d+\\1\\d+\\1\\d+";
    return test.matches(regex);
}

Here I have used regex : \d+(\D+)\d+\1\d+\1\d+ It is equivalent to : -

  \d+          (\D+)       \d+         \1          \d+          \1         \d+
numbers_1 non-numbers_1 numbers_2 non-numbers_1 numbers_3 non-numbers_1 numbers_4

Or you can further reduce the above regex to \d+(\D+)\d+(\1\d+){2}

6 Comments

1231231128.123.23.121212312 , it will say this ip is valid.
The above answer is for : How do i check to see if there are repeated patterns in a string
you said valid, then what it means 0 to 255 ?
please read my above comment. I am saying this is to check repeated pattern.
And I posted this answer because as you can see nobody marked this, and in their answer they are saying 12x23.34-45 as valid. And I have not edited my answer. it will still say valid to 1231231128.123.23.121212312. @İlkerKorkut
|
0

This should help

import java.util.regex.Pattern;

public class App
{
    private static String IPV4_REGEX ="^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
    private static final Pattern IP4_PATTERN = Pattern.compile(IPV4_REGEX);

    public static void main( String[] args ) {
        String test1 = "198<string>12<string>88<string>201";
        String test2 = "198(foo)12(foo)88(foo)201";
        if(isIP(test1)) {
            System.out.println("Yep, it's an ip");
        }
        if(isIP(test2)) {
            System.out.println("Yep, it's an ip");
        }
    }

    public static boolean isIP(String input) {

        String[] chunks = input.replaceAll("[^\\d.]", "x").split("x+");
        if (chunks.length != 4) {
            System.out.println("not valid ");
            return false; 
        }

        String ip = chunks[0] + "." + chunks[1] + "." + chunks[2] + "." + chunks[3];
        return IP4_PATTERN.matcher(ip).matches();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.