Given a string, extract the first letter of each word in it. “Words” are defined as contiguous strings of alphabetic characters i.e. any upper or lower case characters a-z or A-Z. Examples:
Input : Geeks for geeks
Output :Gfg
        
Input : United Kingdom
Output : UK
Below is the Regular expression to extract the first letter of each word. It uses ‘/b'(one of boundary matchers). Please refer How to write Regular Expressions? to learn it.
\b[a-zA-Z]
Java
| // Java program to demonstrate extracting first// letter of each word using Regeximportjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassTest {    staticvoidprintFirst(String s)    {        Pattern p = Pattern.compile("\\b[a-zA-Z]");        Matcher m = p.matcher(s);        while(m.find())            System.out.print(m.group());        System.out.println();    }    publicstaticvoidmain(String[] args)    {        String s1 = "Geeks for Geeks";        String s2 = "A Computer Science Portal for Geeks";        printFirst(s1);        printFirst(s2);    }} | 
GfG ACSPfG
This article is contributed by Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

 
                                    







