2

I have a string like this,

["[number][name]statement_1.","[number][name]statement_1."]

i want to get only statement_1 and statement_2. I used tried in this way,

String[] statement = message.trim().split("\\s*,\\s*");

but it gives ["[number][name]statement_1." and "[number][name]statement_2."] . how can i get only statement_1 and statement_2?

4 Answers 4

3

Match All instead of Splitting

Splitting and Match All are two sides of the same coin. In this case, Match All is easier.

You can use this regex:

(?<=\])[^\[\]"]+(?=\.)

See the matches in the regex demo.

In Java code:

Pattern regex = Pattern.compile("(?<=\\])[^\\[\\]\"]+(?=\\.)");
Matcher regexMatcher = regex.matcher(yourString);
while (regexMatcher.find()) {
    // the match: regexMatcher.group()
} 

In answer to your question to get both matches separately:

Pattern regex = Pattern.compile("(?<=\\])[^\\[\\]\"]+(?=\\.)");
Matcher regexMatcher = regex.matcher(yourString);
if (regexMatcher.find()) {
    String theFirstMatch: regexMatcher.group()
} 
if (regexMatcher.find()) {
    String theSecondMatch: regexMatcher.group()
} 

Explanation

  • The lookbehind (?<=\]) asserts that what precedes the current position is a ]
  • [^\[\]"]+ matches one or more chars that are not [, ] or "
  • The lookahead (?=\.) asserts that the next character is a dot

Reference

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

6 Comments

FYI: added regex demo, explanation and reference. Please let me know if you have questions.:)
why are you use while(){} loop ??
The find() method only returns one match. To get all the matches, we iterate with a while loop. This is the standard way of getting all matches in Java. :)
how can i get statement_1 and statement_2 seperately ??
Added a section called In answer to your question to get both matches separately Please let me know if this works for you.
|
2

I somehow don't think that is your actual string, but you may try the following.

String s = "[\"[number][name]statement_1.\",\"[number][name]statement_2.\"]";
String[] parts = s.replaceAll("\\[.*?\\]", "").split("\\W+");
System.out.println(parts[0]); //=> "statement_1"
System.out.println(parts[1]); //=> "statement_2"

4 Comments

print not gives statement_1 and statement_2 only. it gives [string, string,...] like this
how can i get statement_1 and statement_2 separately ??
but the problem is statement_1 and statement_2 contains more than one word. they are statements.
Well next time, perhaps it would be great to post your actual string data.
1

is the string going to be for example [50][James]Loves cake?

    Scanner scan = new Scanner(System.in);

    System.out.println ("Enter string");

    String s = scan.nextLine();

    int last = s.lastIndexOf("]")+1;

    String x  = s.substring(last, s.length());

    System.out.println (x);


Enter string
[21][joe]loves cake
loves cake

Process completed.

1 Comment

it's formatted weird but try it let us know if that's what you wanted
0

Use a regex instead.

With Java 7

    final Pattern pattern = Pattern.compile("(^.*\\])(.+)?");

    final String[] strings = { "[number][name]statement_1.", "[number][name]statement_2." };

    final List<String> results = new ArrayList<String>();

    for (final String string : strings) {
        final Matcher matcher = pattern.matcher(string);
        if (matcher.matches()) {
            results.add(matcher.group(2));
        }
    }

    System.out.println(results);

With Java 8

    final Pattern pattern = Pattern.compile("(^.*\\])(.+)?");

    final String[] strings = { "[number][name]statement_1.", "[number][name]statement_2." };

    final List<String> results = Arrays.stream(strings)
            .map(pattern::matcher)
            .filter(Matcher::matches)
            .map(matcher -> matcher.group(2))
            .collect(Collectors.toList());

    System.out.println(results);

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.