1

String: [img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]

Result I Want: [img border=0]images/bbcode/sets/misc/bullet_go.png[/img] without /scm/ text.

Issue: Text scm is not static, could be any other text in data.

What I want: Have a look to this string
[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]
Regex which can fetch a text between ] and images/bbcode/ so the regex will detect the \scm\ text and then can remove this \scm\ from String data and end result will look like [img border=0]images/bbcode/sets/misc/bullet_go.png[/img]

PS: I am implementing this logic in Java.

9
  • 3
    Yeah, where's your code so far? Commented May 22, 2020 at 8:08
  • 1
    is [img border=0] static? Commented May 22, 2020 at 8:09
  • 1
    Is images/bbcode/ static? Commented May 22, 2020 at 8:16
  • @MajidRoustaei yes this is static Commented May 22, 2020 at 8:23
  • 1
    So you want to remove all the text between [img border=0] and images/bbcode/, correct? Commented May 22, 2020 at 8:25

4 Answers 4

2

you can reach the goal without using regex, too.

since you said that the other parts are static, try this:

String myStr = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
myStr = "[img border=0]" + myStr.substring(myStr.indexOf("images"));
System.out.println(myStr);

and the output will be:

[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't the question ask for a regular expression, which I don't think your answer provides. Or maybe you should mention that a regex is not required in order to solve the problem?
To make this answer more generic use: myStr = "[img border=0]" + myStr.substring(myStr.indexOf("/", myStr.indexOf("/") + 1) + 1);
Thanks for your helpful feedback guys . @Abra, @DavidDr90. your command causes a small bug where there be a / in the dynamic part.
1

I have captured text between '] and /images..' and replace this text with "". Check the following demo:

String s = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
s = s.replaceAll("(?<=])/[^/]+/","");
System.out.println(s);

Comments

1

if [img border=0] dynamic, you can take all except /scm/

some demo

   String input = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";

    Pattern p = Pattern.compile("(^.*\\])\\/.*?\\/(.*$)");

    Matcher m = p.matcher(input);
    if (m.find()) {
        String output = m.replaceFirst("$1$2"); 
        System.out.println(output);
    }



 // -> [img border=0]images/bbcode/sets/misc/bullet_go.png[/img]

Comments

0

I found one more way to solve this same problem

String pattereString = "].*/images";

        String maineString = "[img border=0]/scm/images/bbcode/sets/misc/bullet_go.png[/img]";
        maineString = maineString.replaceAll(pattereString, "images");
        System.out.println(maineString);

Output:

[img border=0]images/bbcode/sets/misc/bullet_go.png[/img]

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.