0

Im trying to parse a string thats like this;

/^/^this is a big|/*this is a bold text|/-this is strike through| i need the text between $/^ and $|

Tested the regex at regexr and java regex tester and both of them shows its working.

Also referenced to: Java regex matcher always returns false and Regex that always returns false

Compared my code with Java Regex tutorial on Jenkov and it seems to be simillar

My Code

 public static final String bold = "/\\/\\*(.*?)\\|/g";
    public static final String strike = "/\\/\\-(.*?)\\|/g";
    public static final String big = "/\\/\\^(.*?)\\|/g"; 


    String input = "/^/^this is a big|/*this is a bold text|/-this is strike through|";

    SpannableStringBuilder str = new SpannableStringBuilder(input.trim());
    Pattern pattern = big;
    Matcher matcher = pattern.matcher(str.toString());

            while (matcher.find()) {
                  str.setSpan(new RelativeSizeSpan((1.25f)),
                        matcher.start(), matcher.end(),
                        SPAN_INCLUSIVE_INCLUSIVE);
                //input.replace(matcher.group(1),"");
            }

so matcher.find() returns false and there arent any matcher.groups() either. I really have no idea where i am slipping up here.

EDIT

Pattern pattern = Pattern.compile(big);

I forgot to add that part since the regex itself was a return from another function which returns an already compiled pattern. Thanks for all of your help.

1
  • 2
    How is your code even compiling. You are assigning a String to a Pattern which won't even compile. Also in Java, you don't need to have / in regex like it is in other languages like JavaScript. Commented Feb 16, 2019 at 9:58

1 Answer 1

2
    Pattern pattern = Pattern.compile("/\\^(.*?)\\|");

    String input = "/^/^this is a big|/*this is a bold text|/-this is strike through|";
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        System.out.println("Found: " + matcher.group(1));
    }
    System.out.println("That’s all, folks");

Output is:

Found: /^this is a big
That’s all, folks

If you wanted to delete the text between /^ and |, use the idiom given in the documentation of Matcher.appendReplacement():

    StringBuilder sb = new StringBuilder();
    while (matcher.find()) {
        matcher.appendReplacement(sb, "/^|");
    }
    matcher.appendTail(sb);
    System.out.println(sb.toString());  
/^|/*this is a bold text|/-this is strike through|

Why didn’t your code work?

Look more closely at the Jenkov tutorial. It has this code line:

    String patternString = "is";

The pattern doesn’t begin with / and doesn’t end with /g. So by including these parts you are requiring them to be included in the matched text. Since they were not in your input string, your pattern could not match. Also the call to Pattern.compile was missing from your code. It is in this code line in the tutorial:

    Pattern pattern = Pattern.compile(patternString);

Link: documentation of Matcher.appendReplacement()

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

1 Comment

V.V thanks. realized the / and /g was the problem, the editor at RegExr had those. this solved the problem!

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.