2

I have a text file which has some data like this:

- data: {text: '=', name: '10', id: 316, row: 8, column: 1, width: 19, height: 1}

I want to replace the '=' with = and '10' with 10.

I have tried using

Pattern p= Pattern.compile("\\w+:\\s\\'(.*)\\'"); 
matcher.group(1);

This gives me =', name: '10

But I need to get =.

How do I find all the matches?

3 Answers 3

3

I want to replace the '=' with = and '10' with 10

You can probably do:

data = data.replaceAll("'([^']*)'", "$1");

to string all strings from single quote.

OR make it more restrictive by replacing only 10 OR = only:

data = data.replaceAll("'(10|=)'", "$1");
Sign up to request clarification or add additional context in comments.

Comments

1

Is RegEx really required here? If all you're trying to do is just replace those 2, perhaps you should try something like:

string = string.replace("'", "");

I am assuming that you want to replace all of the values that are contained with in a '.

Or if you just want to replace only those 2 occurrences, feel free to try something like:

string = string.replace("'='", "=").replace("'10'", "10"); 

4 Comments

that helps but if i have text like say text: message to 'Josh' then this should not be replaced
@Raghav Then the latter solution (string = string.replace("'='", "=").replace("'10'", "10");) is definitely what you are looking for.
its a text file i am using and we are not sure what data come in and the file ranges around 7 to 8 mb
@JoshM you could enforce escape sequences in the strings that contain a '. It's what has been done for a while and is probably preferred due to the simplicity of the implementation.
0

Actually for thing you need it is very very simple :

    String change = "text: '=', name: '10', id: 316, row: 8, column: 1, width: 19, height: 1";
    String newString = change.replaceAll("'", "");

1 Comment

that helps but if i have text like say text: message to 'libik' then this should not be replaced

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.