Pattern p = Pattern.compile("[\\w\\.]+\\sat\\s[\\w\\.]+");
Will match the following three lines from a web page:
dave.andersen at gmail.com
appear at SOSP
staring at now
However, I only want the first line to be matched. Is there a way to specify that there be at least one dot on both sides of at?
You’ve got the right idea using “+” to indicate at least one, but using “[\w\.]+” matches any string of word characters or periods.
This will match things like “foo.bar at foo.bar” or “foo….bar at foo..bar”:
This will match things like “foo.bar at foo.bar” or “foo.bar.bar at foo.foo.bar”:
The difference between “\w+\.+\w+” and “[\w\.]+” is that the first form will match at least one word character followed by at least one period followed by at least one word character whereas the second form will match any mix up of word characters and periods.