0

I m having a string as below

  This is a test\nAnother test\n#art\n#paintings#collections

From which i have pick the words - art, paintings,collections.

I have written a java program for that here. The code -

  String str = "This is a test\nAnother test\n#art\n#paintings#collections";


    String tag_name ="";
    String[] sp = str.split(" |\n");
    for (int j =0; j<sp.length; j++) {
        //System.out.println(""+sp[j]);
        if ( String.valueOf(sp[j].charAt(0)).equals("#")) {                
            tag_name = sp[j];
            String[] np = tag_name.split("#");
            for (int k = 0; k<np.length; k++) {
                if(np[k].length() >0 ) {
                    tag_name = np[k].replaceAll("\n", "");
                    System.out.println(""+ np[k]);
                }
            }                
            //System.out.println("" + tag_name);
        }
    }

Please suggest how can i do this using a more strong regex code.

2
  • It's not clear what you're asking here. What is wrong with the above code? Commented Feb 23, 2014 at 17:08
  • it is having split two times and two for loops. i thought if it can be reduced. Commented Feb 23, 2014 at 17:10

2 Answers 2

1

If I'm understanding your requirements, you want to find all words following #. If so, this works:

   import  java.util.regex.Matcher;
   import  java.util.regex.Pattern;
/**
   <P>{@code java ArtTypesXmpl}</P>
 **/
public class ArtTypesXmpl  {
   public static final void main(String[] igno_red)  {
     String sToSearch = "This is a test\nAnother test\n#art\n#paintings#collections";

     Matcher mHashThenWord = Pattern.compile("#(\\w+)").matcher(sToSearch);

     while(mHashThenWord.find())  {
        System.out.println(mHashThenWord.group(1));
     }
   }
}

Output:

[C:\java_code\]java ArtTypesXmpl
art
paintings
collections

JavaDoc on Pattern and Matcher: http://docs.oracle.com/javase/7/docs/api/java/util/regex/package-summary.html

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

1 Comment

this is really cool. Never quite seen all this magnificence anywhere :)
0

Try with

String[] sp = str.split("\\s|\\n");

From Here

Twelve characters have special meanings in regular expressions: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {. These special characters are often called metacharacters.

1 Comment

Escaping those characters isn't necessary, but at least credit the place you're lifting that text from.

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.