views:

54

answers:

6

What are the string patterns that we can use with findInLine() method. For example if input is an email address e.g [email protected], how do i find only "vinny.kinny" from the input using the findInLine() method in java.

+1  A: 

look at regular expression in java:

http://java.sun.com/developer/technicalArticles/releases/1.4regex/

mohammad shamsi
+1  A: 

When you write about a method, it is better to specify in which class (of which package/library, etc.) it can be found.
I don't know the whole Java API, far from it, but I never heard of a findInLine() method.
Unless you meant to write this method yourself, this isn't very clear...

If you plan to write this method, you can use any method in String class. For example, indexOf() can allow you to find the index of "@" and you can then use the substring() method to extract the part before it.
Or you can use regular expressions if you need lot of flexibility.

PhiLho
+1  A: 

No a direct ansewr to your question but I'll use a StringTokenizer for that:

new StringTokenizer(emailString, "@");
Manuel Selva
Quote, javadoc: *"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code."* - `String.split()` is the recommended way to split Strings
Andreas_D
the Scanner class should be used for tokenizing now..
atamanroman
Thanks for your comments, I was not aware about that. I should read more carefully the Javadoc of all the classes I am using.
Manuel Selva
+1  A: 

Assuming that you are talking about the findInLine() method of the Scanner class, those are regex patterns as described in the Javadoc for java.util.regex.Pattern.

Michael Borgwardt
A: 

Sanner#findInLine(String regExp) takes the same pattern like String#split(String regExp). It is a regular expression (Java style) and a good documentation can be found in the javaDoc of the Pattern class.

Andreas_D
A: 

This is what i did:

import java.util.*; import java.util.regex.Pattern;

public class GetUserName {

public static void main(String[] args) { Scanner scn = new Scanner(System.in); String userName;
System.out.println("Enter your email address: "); userName = scn.findInLine(Pattern.compile("[a-z]+"));

System.out.println(userName); }

}

The output from the console window:

Enter your email address:

[email protected]

thyaga

--------------------->

This is what i learnt: the Pattern class, FindInLine method and how to use the pattern parameter, regex constructs.

Thank you all, thanks again for the help you provided !

PS: the regex for the email is not complete and has to be refined.

thyaga