1

I have a java file which I got from android, this has some hard coded values in it. Basically we have a file which creates an object of type country and adds it to a list.

I wish to attain the same functionality but I am not sure of the reg-ex required to find and replace the file contents. Basically this is how one of the lines in it looks like...

countries.add(new Country("af", "Afghanistan", 93));

And this is what I wan't it to look like

[countries addObject:[[Country alloc] initWithArray:@[@"af",@"Afghanistan",@"93"]];

Do you think regex can be used for such extensive case..? Or will I have to manually do this for every entry..?

1 Answer 1

5

You can do a literal search and capture groups like this and replace them.

Regex: countries\.add\(new Country\("([a-z]+)", "(.*)", (\d+)\)\);

Replacement to do: [countries addObject:[[Country alloc] initWithArray:@[@"$1",@"$2",@"$3"]];

Regex101 Demo

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

2 Comments

but there is an issue here, xcode doesn't seem to be recognising groups, instead it adds @"1",@"2",@"3"
Changed it to work on xcode and include all characters.

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.