Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

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

share|improve this answer
    
FYI: added regex demo, explanation and reference. Please let me know if you have questions.:) –  zx81 Jul 19 '14 at 4:22
    
why are you use while(){} loop ?? –  user3800832 Jul 19 '14 at 5:25
    
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. :) –  zx81 Jul 19 '14 at 5:27
    
how can i get statement_1 and statement_2 seperately ?? –  user3800832 Jul 19 '14 at 5:37
    
Added a section called In answer to your question to get both matches separately Please let me know if this works for you. –  zx81 Jul 19 '14 at 5:44

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"
share|improve this answer
    
print not gives statement_1 and statement_2 only. it gives [string, string,...] like this –  user3800832 Jul 19 '14 at 5:04
    
@user3800832 I edited it for a loop. You need to post your actual string data... –  hwnd Jul 19 '14 at 5:08
    
how can i get statement_1 and statement_2 separately ?? –  user3800832 Jul 19 '14 at 5:27
    
@user3800832 see updated edit. –  hwnd Jul 19 '14 at 5:52
    
but the problem is statement_1 and statement_2 contains more than one word. they are statements. –  user3800832 Jul 19 '14 at 6:05

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.

share|improve this answer
    
put ^ in comments –  sunbabaphu Jul 19 '14 at 4:17
    
can't not enough points..... :( –  joe Jul 19 '14 at 4:22
    
yes it's like as you said –  user3800832 Jul 19 '14 at 4:23
    
it's formatted weird but try it let us know if that's what you wanted –  joe Jul 19 '14 at 4:28

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);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.