0

I begin with regex and i want extract values from a String like this

String test="[ABC]Name:User:Date: Adresse ";

I want extract Name, User , Date and Adresse I can do the trick with substring and split

String test = "String test="[ABC]Name:User:Date: Adresse ";
        String test2= test.substring(5,test.length());
        System.out.println(test2);
        String[] chaine = test2.split(":");
        for(String s :chaine)
        {
            System.out.println("Valeur " + s);
        }

but i want try with regex , i did

pattern = Pattern.compile("^[(ABC)|:].");

but it doesn ' t work

Can you help me please ?

Thanks a lot

2 Answers 2

1

String#split is really the best way to accomplish what you are trying to do. Having said that, with regex, the following will give you the same output:

    Pattern p = Pattern.compile("^(?:\\[ABC\\])([^:]+):([^:]+):([^:]+):([^:]+)$");
    Matcher m = p.matcher(test);
    while (m.find()) {
        System.out.println("Valeur " + m.group(1)); // Name
        System.out.println("Valeur " + m.group(2)); // User
        System.out.println("Valeur " + m.group(3)); // Date
        System.out.println("Valeur " + m.group(4)); // Address
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Ok thanks for your help , but if i want to negate [ABC] in order to get only Name how can i do that ?
Not sure what you are asking for, but I've updated the code so that each group will map to a single element in the original string. Basically, when you wrap an expression with (), you capture that group, and you can fetch it later with Matcher.
Thanks a lot! It work perfectly but the regex became more complicated :p. I need practices so i will read some tutorials about regex . btw thanks you again !
You'll capture the empty spaces too. There are some spaces around the address in the example, which I guess you would like to eliminate. You could fix it with :\s*([^:]+)\s*$. It still keeps the spaces inside the address. Also, I'm a bit fuzzy about the example: is it really starting with [ABC] or does it include all the rest. If it's the latter it gets complicated with the start ^ and end $.
Ok , thanks toto, i will update the code with your advices . Yeah , my String begin with[ABC] , here is an exemple [ABC]STRUTS:PAUL:25/06/2011T10H12:8 Rue Pierre de Coubertin 95300 Pontoise" . I must just extract the values between ":" without [ABC]
|
0

You have to escape the [ and ] here is a working example.

^\[(.*)\](.*):(.*):(.*):(.*)$

Note that your code is probably more easily maintained than regular expressions in cases where the regular expression becomes complex.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski

1 Comment

Thanks for the advice and your citation is really correct but i want juste learn a little regex in order to practice

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.