Divide the string by \ b, but not by \ b between the substring

How do you split String into words but leave some phrases / terms intact? I have it right now String[] strarr = str.split("\\b");, but I want to change the regex parameter to accomplish the above. solution should not include regex

For example, if str is equal "The city of San Francisco is truly beautiful!", and the term is "San Francisco", how do you split str so that the resulting String [] array looks like that ["The", "city", "of", "San Francisco", "is", "truly", "beautiful!"]:?


After watching @Radiodef's comment, I decided that I really didn't need a regex. If someone can help me solve this problem, help is still very much appreciated!

+4
source share
4 answers

, . , , .

,

 String[] find(String m[], String c[], String catchStr){

    String comp = c[0];
    ArrayList<String> list = new ArrayList<String>();
    for(int i=0;i<m.length;i++){

        boolean flag = false;

        //comparing if the substring matches or not
        if(comp.equals(m[i])){
            flag = true;
            for(int j=0;j<c.length;j++){
                //you can use equalsIgnoreCase() if you want to compare the string 
                //ignoring the case
                if(!m[i+j].equals(c[j])){
                    flag = false;
                    break;
                }
            }

        }

        if(flag){
            list.add(catchStr);
            i = i + c.length-1;
        }else{
            list.add(m[i]);
        }

    }

    //converting result into String array
    String finalArr[] = list.toArray(new String[list.size()]);

    return finalArr;

}

,

String mainStr = "The city of San Francisco is truly beautiful!";
String catchStr = "San Francisco";
String mainStrArr[] = mainStr.split(" ");
String catchStrArr[] = catchStr.split(" ");

String finalArr[] = find(mainStrArr, catchStrArr, catchStr);
+1

, , , .

, :

([A-Z][a-z]*(?:\s?[A-Z][a-z]+)*|[a-z!]+)

MATCH 1
1.  [0-3]   `The`
MATCH 2
1.  [4-8]   `city`
MATCH 3
1.  [9-11]  `of`
MATCH 4
1.  [12-25] `San Francisco`
MATCH 5
1.  [26-28] `is`
MATCH 6
1.  [29-34] `truly`
MATCH 7
1.  [35-44] `beautiful!`

Java

String line = "The city of San Francisco is truly beautiful!";
Pattern pattern = Pattern.compile("([A-Z][a-z]*(?:\\s?[A-Z][a-z]+)*|[a-z!]+)");
Matcher matcher = pattern.matcher(line);

while (matcher.find()) {
    System.out.println("Result: " + matcher.group(1));
}
+1

- ,

    String[] a = str.split("(?<!San)\\s+(?!Francisco)");

, ,

    String str = "The city of San Francisco is truly beautiful!";
    String[] exclusions = { "San Francisco", "Los Angeles" };
    List<String> l = new ArrayList<>();
    Matcher m = Pattern.compile("\\w+").matcher(str);
    while (m.find()) {
        l.add(m.group());
        for (String ex : exclusions) {
            if (str.regionMatches(m.start(), ex, 0, ex.length())) {
                l.set(l.size() - 1, ex);
                m.find();
                break;
            }
        }
    }
    System.out.println(l);
0

, , . , , , , .

    // let say:
    // whole = "The city of San Francisco is truly beautiful!",
    // token = "San Francisco"

    public static String[] excludeString(String whole, String token) {

        // replaces token string "San Francisco" with "SanFrancisco"
        whole = whole.replaceAll(token, token.replaceAll("\\s+", ""));

        // splits whole string using space as delimiter, place tokens in a string array
        String[] strarr = whole.split("\\s+");

        // brings "SanFrancisco" back to "San Francisco" in strarr
        Collections.replaceAll(Arrays.asList(strarr), token.replaceAll("\\s+", ""), token);

        // returns the array of strings
        return strarr;
    }

:

    public static void main(String[] args) {

        String[] arr = excludeString("The city of San Francisco is truly beautiful!", "San Francisco");
        System.out.println(Arrays.asList(arr));

    }

, : "The city of San Francisco is truly beautiful!"

: [The, city, of, San Francisco, is, truly, beautiful!]

0

All Articles