get Word from string using regex - Java java.util.regex

Java examples for java.util.regex:Match Word

Description

get Word from string using regex

Demo Code

/**//from  w w  w  .j  a va2s .  c o  m
 * 
 * Copyright (c) 2000-2014 All Rights Reserved.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String word = "java2s.com";
        System.out.println(getWord(word));
    }

    public static String getWord(String word) {
        String wor = "";
        //        .+?/\\p{Alnum}+?
        if (word.matches(".+?/\\p{Alnum}+?")) {
            wor = word.substring(0, word.lastIndexOf("/"));
        }

        return wor;
    }
}

Related Tutorials