Prathamesh dhanawade
Prathamesh dhanawade

Reputation: 513

Get string within double quotes along with rest of the string

I have a case where I need to extract the string within double quotes in one var and the rest of the string in another var.

Two possibilities:

String: "Franklin B" Benjamin

Result:

var1 = Franklin B
var2 = Benjamin

String: Benjamin "Franklin B"

Result:

var1 = Benjamin
var2 = Franklin B

Regex/Without regex; I am open to any method.

Upvotes: 0

Views: 1043

Answers (4)

Prathamesh dhanawade
Prathamesh dhanawade

Reputation: 513

@Shar1er80 Nice piece of work without regex. Worked great.

I also tried with regex:

//Using regex to get values separated by whitespace but keeping values with double quotes

RegexOptions options = RegexOptions.None;
Regex regex = new Regex( @"((""((?<token>.*?)(?<!\\)"")|(?<token>[\w]+))(\s)*)", options );

string input = @"   Here is ""my string"" it has   "" six  matches""   ";
var result = (from Match m in regex.Matches( input ) 
              where m.Groups[ "token" ].Success
              select m.Groups[ "token" ].Value).ToList();

Gave me exact result.

Upvotes: 0

Shar1er80
Shar1er80

Reputation: 9041

Give this a try...

Basically you remove any leading delimiter in the string before you perform the split. This way you don't have to worry about a leading empty element.

public static void main(String[] args) {
    String testString = "\"Franklin B\" Benjamin";
    String testString2 = "Benjamin \"Franklin B\"";

    displaySplitResults(mySplit(testString, "\""));
    displaySplitResults(mySplit(testString2, "\""));
}

private static String[] mySplit(final String input, final String delim)
{
    return input.replaceFirst("^" + delim, "").split(delim);
}

private static void displaySplitResults(String[] splitResults) {
    if (splitResults.length == 2) {
        String var1 = splitResults[0].trim();
        String var2 = splitResults[1].trim();

        System.out.println(var1);
        System.out.println(var2);
    }        
}

Results:

Franklin B
Benjamin
Benjamin
Franklin B

Upvotes: 1

Patrick van Deudekom
Patrick van Deudekom

Reputation: 155

The following will get you a List with the values you want:

private List<String> getValues(String input) {
        List<String> matchList = new ArrayList<>();
        Pattern regex = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'");
        Matcher regexMatcher = regex.matcher(input);
        while (regexMatcher.find()) {
            matchList.add(regexMatcher.group());
        }
        return matchList;
    }

Taken from Regex for splitting a string using space when not surrounded by single or double quotes

Upvotes: 0

Bubletan
Bubletan

Reputation: 3863

A simple non-regex way to do it:

public static String[] split(String input) {
    if (input.charAt(0) == '"') {
        return input.substring(1).split("\" ");
    } else {
        return input.substring(0, input.length() - 1).split(" \"");
    }
}

First check whether the first character is ". Then remove the quote from either beginning or the end and simply split it.

Upvotes: 0

Related Questions