2

I have a string:

hello example >> hai man

How can I extract the "hai man" using Java regex or another technique?

1
  • 1
    @Tassos: "The homework tag ... is now discouraged," but, @subha, please (as always) follow general guidelines: state any special restrictions, show what you've tried so far, and ask about what specifically is confusing you. Commented Nov 11, 2010 at 13:10

6 Answers 6

4

You can use regex as:

String str    = "hello example >> hai man";
String result = str.replaceAll(".*>>\\s*(.*)", "$1");
Sign up to request clarification or add additional context in comments.

Comments

2

See run: http://www.ideone.com/cNMik

public class Test {
    public static void main(String[] args) {
        String test = "hello example >> hai man";

        Pattern p = Pattern.compile(".*>>\\s*(.*)");
        Matcher m = p.matcher(test);

        if (m.matches())
            System.out.println(m.group(1));
    }
}

Comments

1

In Java 1.4:

 String s="hello example >> hai man";
 String[] b=s.split(">>");
 System.out.println(b[1].trim());

Comments

1

The most basic way is to play with character from String and its index.

For the hello example >> hai man use

String str ="hello example >> hai man";
int startIndex = str.indexOf(">>");
String result = str.subString(startIndex+2,str.length());  //2 because >> two character 

I think it clears up basic idea.

There are lots of tricks you can parse using

Another simpler way is :

     String str="hello example >> hai man";
     System.out.println(str.split(">>")[1]);

Comments

0

Consider the following:

public static void main(String[] args) {
        //If I get a null value it means the stringToRetrieveFromParam does not contain stringToRetrieveParam.
        String returnVal = getStringIfExist("hai man", "hello example >> hai man");
        System.out.println(returnVal);

        returnVal = getStringIfExist("hai man", "hello example >> hai man is now in the middle!!!");
        System.out.println(returnVal);
    }

    /**Takes the example and make it more real-world like.
     * 
     * @param stringToRetrieveParam
     * @param stringToRetrieveFromParam
     * @return
     */
    public static String getStringIfExist(String stringToRetrieveParam,String stringToRetrieveFromParam){
        if(stringToRetrieveFromParam == null || stringToRetrieveParam == null){
            return null;
        }
        int index = stringToRetrieveFromParam.indexOf(stringToRetrieveParam);
        if(index > -1){
            return stringToRetrieveFromParam.substring(index,index + stringToRetrieveParam.length());
        }
        return null;
    }

Comments

-2

what i understand that you wana eleminate this u can do some thing like

string myString = "hello example >> hai man";
string mySecondString = mystring.Replace("hai man", ""); 

You will get only hello example >>

1 Comment

I believe you have misunderstood the question.

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.