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.
look at regular expression in java:
http://java.sun.com/developer/technicalArticles/releases/1.4regex/
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.
No a direct ansewr to your question but I'll use a StringTokenizer for that:
new StringTokenizer(emailString, "@");
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
.
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.
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:
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.