My Sites


Wednesday, June 3, 2015

How to extract word from string?

String str = "blabla http://www.mywebsite.com blabla";
String regex = "((https?:\\/\\/)?(www.)?(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_/.0-9#:+?%=&;,]*)?)?)";
Matcher m = Pattern.compile(regex).matcher(str);
if (m.find()) {
    String url = m.group(); //value "http://www.mywebsite.com"
}
 
  try {
 sc2 = new Scanner(new File("src/main/resources/text.txt"));
                   while (sc2.hasNextLine()) {
            String line = sc2.nextLine();
            String clientString = StringUtils.substringBetween(line, "(", ")");
            System.out.println( i+ " "+clientString);
            i++;
      }      
} catch (FileNotFoundException e) {
 } 
 
 <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.4</version>
 </dependency>
 

No comments:

Post a Comment