Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the first letter of each word in a string using regex

Tags:

java

regex

I'm trying to get the first letter of each word in a string using regex, here is what I have tried:

public class Test
{
    public static void main(String[] args)
    {
        String name = "First Middle Last";
        for(String s : name.split("(?<=[\\S])[\\S]+")) System.out.println(s);
    }
}

The output is as follows:

F
 M
 L

How can I fix the regex to get the correct output?

like image 575
Eng.Fouad Avatar asked Sep 27 '11 18:09

Eng.Fouad


People also ask

How do you get the first character of each word in a string?

To get the first letter of each word in a string: Call the split() method on the string to get an array containing the words in the string. Call the map() method to iterate over the array and return the first letter of each word. Join the array of first letters into a string, using the join() method.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I print the first letter of a string?

Get the first character of a string in python As indexing of characters in a string starts from 0, So to get the first character of a string pass the index position 0 in the [] operator i.e. It returned a copy of the first character in the string. You can use it to check its content or print it etc.

How do I find the first character in a regular expression?

The regular expression to match String which contains a digit as first character is “^[0-9]. *$”. Pass this as a parameter to the matches() method of the String class.

How to get the first letter of every word in a string?

It is here: How to Get First Letter of every word in a String As-is, that code will return an array of strings, where each string is just the first character. If you want it to be a single string at the end, then just append a .Join () at the end of it Thank you Dave! var106DeathCity.Split (" "c).

How to get the first letter of each word in Java?

Get the first letter of each word in a string using regex in Java. 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.

How do you use a regex search?

The regex works by serching for single word characters (\w) that have one or more space characters (\s+) or are at the start of a line (^). To modify what is being searched for, the \w can be changed to other regex valid entries. To modify what precedes the search character, modify (\s+|^).

How to get the first character of a string in JavaScript?

Hi @aliaga try like this first split the string using space and then you will get array of words you will get once you get the word then use substring to get the first character from that word and keep looping that words. Try with the following code, "First Name of string".Split (" "c). [Select] (Function (x) x.ToUpper.First ()).ToArray ()


3 Answers

Edit Took some suggestions in the comments, but kept the \S because \w is only alpha-numeric and might break unexpectedly on any other symbols.

Fixing the regex and still using split:

name.split("(?<=[\\S])[\\S]*\\s*")
like image 63
Jacob Eggers Avatar answered Sep 22 '22 10:09

Jacob Eggers


Why not simply:

public static void main(String[] args)
{
    String name = "First Middle Last";
    for(String s : name.split("\\s+")) System.out.println(s.charAt(0));
}   
like image 20
NPE Avatar answered Sep 24 '22 10:09

NPE


(Disclaimer: I have no experience with Java, so if it handles regexes in ways that render this unhelpful, I apologize.)

If you mean getting rid of the spaces preceding the M and L, try adding optional whitespace at the end

(?<=[\\S])[\\S]+\\s*

However, this may add an extra space in the case of single-letter words. This may fix that:

(?<=[\\S])[\\S]*\\s*
like image 24
glibdud Avatar answered Sep 23 '22 10:09

glibdud