0

May I know how to extract the following data using REGEX?

1) textA;textB;textC;textD

Extract "textA;textB;textC" aka parent of textD

2) textA;textB;textC;textD AB(numberA)

Extract "textA;textB;textC;textD" aka parent of AB(numberA)

3) textA;textB;textC;textD AB(numberA) Extract "numberA" for comparison

currently implementation, i use java string function, which make it un-configurable. I suspect the user didn't give me the actual data and I need to change the function again in the near future. I hope to use regex to make the function configurable.

2 Answers 2

3
  1. (.*);[a-zA-Z]+ - $1
  2. (.*) .* - $1
  3. .* .*\((.*)\) - $1
    How to use regexes and groups: http://www.javamex.com/tutorials/regular_expressions/capturing_groups.shtml
    Example:

    String s = "textA;textB;textC;textD";
    Pattern pt = Pattern.compile("(.*);[a-zA-Z]+");
    Matcher mt = pt.matcher(s);
    if(mt.matches())
        System.out.println(mt.group(1));
    

That prints: textA;textB;textC.
UPD: Because the pattern is not known, answer like 1)textA;textB;textC;(textD) is also true. When asking such questions, it is better to write pattern, even if you don't know regexes you can use words only.
UPD: thx for correction

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

3 Comments

care to explain a bit.. I tried using regexplanet.com/advanced/java/index.html for testing It doesn't work.
I haven't tested this, but I think the 3rd regex is incorrect. I think it will match "AB(numberA)", while a match for "numberA" is requested. Shouldn't it be something like .* .*\((.*)\) - $1
for N° 3 : ".*\\(([A-Fa-f0-9 ,.]{1,128}).*\\)*"extract the number (with basic format symbol) remove letters if no hexadecimals are waited and manage other symbols as you like to keep close to the correct transmitted String, you can also adapt size {128} to maximal length of number
0

can simply use this?

String[] arr = "textA;textB;textC;textD AB".spilt("[; ]");

1 Comment

I can't customize it. You know Users?

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.