Java 中 findInLine 方法中的字符串模式
我们可以在 findInLine() 方法中使用哪些字符串模式。例如,如果输入是电子邮件地址,例如 [email protected],如何处理我使用java中的findInLine()方法从输入中仅找到“vinny.kinny”。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看java中的正则表达式:
http://java.sun.com/developer /technicalArticles/releases/1.4regex/
look at regular expression in java:
http://java.sun.com/developer/technicalArticles/releases/1.4regex/
当你写一个方法时,最好指定它可以在哪个类(哪个包/库等)中找到。
我不了解整个 Java API,远非如此,但我从未听说过 findInLine() 方法。
除非您打算自己编写此方法,否则这不是很清楚...
如果您打算编写此方法,则可以使用 String 类。例如,indexOf() 可以让您找到“@”的索引,然后您可以使用 substring() 方法提取它前面的部分。
或者,如果您需要很大的灵活性,您可以使用正则表达式。
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.
没有直接回答你的问题,但我将使用 StringTokenizer 来解决这个问题:
No a direct ansewr to your question but I'll use a StringTokenizer for that:
假设您正在讨论
Scanner
类的findInLine()
方法,这些是 Javadoc 中描述的正则表达式模式
java.util.regex.Pattern
。Assuming that you are talking about the
findInLine()
method of theScanner
class,those are regex patterns as described in the Javadoc for
java.util.regex.Pattern
.Sanner#findInLine(String regExp)
采用与String#split(String regExp)
。它是一个正则表达式(Java 风格),可以在Pattern
类。Sanner#findInLine(String regExp)
takes the same pattern likeString#split(String regExp)
. It is a regular expression (Java style) and a good documentation can be found in the javaDoc of thePattern
class.这就是我所做的:
控制台窗口的输出:
-------------------->
这就是我学到的:Pattern 类、FindInLine 方法以及如何使用模式参数、正则表达式构造。
谢谢大家,再次感谢大家提供的帮助!
PS:电子邮件的正则表达式不完整,需要完善。
This is what i did:
The output from the console window:
--------------------->
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.