0

Can't get my head around this for quite some time already. I have this piece of code:

getStringFromDom(doc).replaceAll("contract=\"\\d*\"|name=\"\\p{L}*\"", "");

Basically I need it to work literally the opposite way - to replace everything BUT the specified regex. I've been trying to do it with the negative lookahead to no avail.

5
  • 3
    Please provide a sample text and expected output. Right now, it sounds as if ideone.com/e6gUWg is all you need. Commented Sep 26, 2017 at 12:35
  • I have an xml, say <AnotherDoc accNum="1111" docDate="2017-09-26" docNum="2222" name="foo"> <anotherTag>some date</anotherTag> etc. I need to write to a file " name="foo" " and nothing else preferably not using matcher Commented Sep 26, 2017 at 12:37
  • Why not use Matcher if it is used by the engine at any rate? Commented Sep 26, 2017 at 12:40
  • 1
    Since your input is XML, it would be much wiser to use an XML parser. JAXP is in the JRE. Jackson is a programmer-friendly alternative. Commented Sep 26, 2017 at 12:52
  • @WiktorStribiżew there's literally no excuse to not use Matcher, I will probably just do it now, replaceAll feels wrong here. I'm a bit tired so I just wanted to use the solution I've already started working on. Commented Sep 26, 2017 at 13:04

3 Answers 3

2

For your particular task, I think

getStringFromDom(doc).replaceAll(".*?(contract=\"\\d*\"|name=\"\\p{L}*\").*", "$1");

should do what you need.

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

2 Comments

If there are both contract and name attributes, this approach will not work as it will be removing 1 attribute then.
It does work, but on one line only, is there any way to replace everything else?
1

You want to remove everything that does not match the pattern. This is the same as simply filtering the pattern matches. Use the regex to find matches for that pattern, then collect the matches in a stringbuilder.

Matcher m = Pattern.compile(your pattern).matcher(your input);
StringBuilder sb = new StringBuilder();
while (m.find()) sb.append (m.group()).append('\n');
String result = sb.toString();

1 Comment

This is right, I've taken Wiktor Stribiżew's solution though and slightly modified it. Matcher is definitely what I wanted to use here.
1

I also think that removing what your are not looking for is a double negative. Concentrate on what you are looking for and use a pattern matching for that. This example searches your document for any name attributes:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        String input = "<AnotherDoc accNum=\"1111\" docDate=\"2017-09-26\" docNum=\"2222\" name=\"foo\"> <anotherTag>some date</anotherTag>";

        Pattern pattern = Pattern.compile("name=\"[^\\\"]*\""); // value are all characters but "
        Matcher matcher = pattern.matcher(input);

        while (matcher.find())
            System.out.println(matcher.group());
    }
}

This prints:

name="foo"

Comments

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.