1

Suppose i have a string " kk a.b.cjkmkc jjkocc a.b.c. jjj 'a.b.ckkkkkkkkkkkkkkkk ' "

I want to replace the substring a.b.c in the string which are only outside the single quote , but it is not working.

Here is my code `

String str = " kk a.b.cjkmkc  jjkocc a.b.c. jjj 'a.b.ckkkkkkkkkkkkkkkk ' ";
 Pattern p = Pattern.compile("a\\.b\\.c");
 Matcher m = p.matcher(str); 
 int x = m.find()
 `
7
  • the questions are different ;) Commented Mar 5, 2014 at 16:55
  • It's not exactly the same - there's the added constraint of the single quotes? Commented Mar 5, 2014 at 16:55
  • I don't think it's an exact duplicate. Apparently he didn't read the other answers and is still making the same mistake, but once he fixes that, he still needs to figure out what to do with single quotes, and the other question doesn't address that. Commented Mar 5, 2014 at 16:56
  • @DNA - yeps you are right Commented Mar 5, 2014 at 16:56
  • 2
    Because there can be any number or pattern of single quotes and target substrings, this is not a regular language and can't be handled with regular expressions. You can handle limited cases, badly, and miss weird corner cases, or you can just break down and write a very simple parser that will be easy to understand and get it right 100% of the time. I hope you make the right decision. Commented Mar 5, 2014 at 18:07

3 Answers 3

1

use this pattern : a\.b\.c(?=(([^']*'){2})*[^']*$) Demo

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

5 Comments

doesn't work on a.b.cxxx'xxx. It is not clear whether the input is always expected to have an even number of quote marks.
@ajb yours won't work on ' a.b.cxxx xxx "outside single quotes" by default means quotes should come in pairs, right?
yes, you're right, my regex won't find a.b.c in that case. And whether quotes "should" come in pairs doesn't really matter; programs have to be able to handle incorrect inputs.
@ajb, in this example 'a'b' which letter is between quotes and which one is not?
That's up to the designer of whoever is writing this software. If this is an incorrect input, they need to plan for this case and make sure it's handled reasonably.
1

To search for a substring outside quotes, you can do something like this:

Pattern pat = Pattern.compile("^(?:[^']|'[^']*')*?a\\.b\\.c");

The first part will skip over:

every character that isn't a quote mark ([^']), or

every sequence of non-quote-mark characters enclosed in quotes ('[^']*').

Once those are skipped, then if it sees the pattern you want, it will know that it isn't inside quote marks.

This will handle a simple case. If things start getting more complicated, e.g. you want to allow \' to quote a quote mark in your input string the way C or Java does in a string literal, the regex starts getting more complicated, and you can quickly reach a point whether either your regex is unreadable or regexes aren't suitable solutions.

EDIT: fixed to put "reluctant" qualifier after second *, so that the first a.b.c will be found.

EDIT 2: If you want to replace the substring you find, it gets trickier. The above pattern matches the entire beginning of the string up through a.b.c, and I couldn't get a look-behind to work so that the match would be only the a.b.c part. I think you'll need to put the beginning of the string in a group, and then use $1 in the replacement string to copy the beginning:

Pattern pat = Pattern.compile("^((?:[^']|'[^']*')*?)a\\.b\\.c");
Matcher m = pat.matcher(source);
if (m.find()) {
    result = m.replaceFirst("$1replacement");
}

I'm not sure replaceAll works with this, so if you want to replace all of them, you may need to loop.

Comments

0

I wouldn't mess with REGEX.

public static void main(String[] args) {

    String str = " kk a.b.cjkmkc  jjkocc a.b.c. jjj 'a.b.ckkkkkkkkkkkkkkkk ' ";
    String[] s = str.split("'");
    str = s[0].replaceAll("[abc]", "") + "'"+ s[1]+"'"
            + s[2].replaceAll("[abc]", "");

    System.out.println(str);
}

OP:

kk ..jkmk  jjko ... jjj 'a.b.ckkkkkkkkkkkkkkkk '

Inefficient.. but works

4 Comments

i want to replace substring , with are outside , not inside the single quote
Do you think for more complex string , will this method work? str = " kk a.b.cjkmkc jjkocc a.b.c. jjj 'a.b.ckkkkkkkkkkkkkkkk ' hfhhhf 'a.b.ckk' a.b.c
what will happen if there is a complex string and you do not know where the 'a.b' is located
@user3275944 - why don't you run this program and see for yourself . :)

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.