Bhagwati Malav
Bhagwati Malav

Reputation: 3549

Regex not giving proper result because of special character

I have input like this :

Helio P10 processor ARM® cortex®-A53™1.8GHz

and using regex : (?i)(Cortex)\\s*-{0,}\\s*(A)?\\s*-{0,}\\s*(\\d{1,2})?

for input eg. Helio P10 processor ARM cortex-A53 1.8GHz it gives proper value - cortex-A53

but for input eg. Helio P10 processor ARM® cortex®-A53™1.8GHz it doesnt work properly and gives cortex as output.

Requirement : To extract values like cortex-A54 but if any special character comes between cortex and - it doesn't work.

Upvotes: 0

Views: 62

Answers (1)

thibsc
thibsc

Reputation: 4059

You can do that:

String s = "Helio P10 processor ARM cortex-A53 1.8GHz"+
        "Helio P10 processor ARM® cortex®-A53™1.8GHz"+
        "Helio P10 processor ARM® Cortex®-A72™1.8GHz";
Pattern p = Pattern.compile("(?i:(?<proc>cortex)(?:.?)-(?<ref>[a-zA-Z]\\d{1,2}))");
Matcher m = p.matcher(s);
while (m.find()){
    System.out.println(m.group("proc")+"-"+m.group("ref"));
}

Regex explanation:

  • (?<proc>cortex): Detect the begin of your wanted result
  • (?:.?): Allow a special char after cortex
  • (?<ref>[a-zA-Z]\d{1,2}): The format of wanted data after cortex
  • (?i:...): Ignore case

Output:

cortex-A53
cortex-A53
Cortex-A72

Upvotes: 1

Related Questions