从String中解析数字和括号?

时间:2016-07-02 21:35:28

标签: java

给定var water_top_full = false; var water_mid_full = false; var water_bottom_full = false; //Make a recursive function that assigns islands to sectors, //and reassigns if an island is assigned to a full sector. var assign_island_sector = function(){ var island_sector_distributor = Math.random(); //Object for island_core var island_core = []; console.log("Island sector distributor: " + island_sector_distributor); console.log("Water top full: " + water_top_full); console.log("Water mid full: " + water_mid_full); console.log("Water bottom full: " + water_bottom_full); if (island_sector_distributor <= (1/3)){ if (water_top_full == false){ island_core[1] = (Math.random() * 100); //Upper/lower boundaries for island core expectation //Change higher or lower based on how close islands get to the shore var core_upper_limit = Math.round(island_core[1] + 50); //Check to make sure that top-sector lower values don't go below 0: var core_lower_limit; if (island_core[1] < 50){ core_lower_limit = Math.round(island_core[1]); } else{ core_lower_limit = Math.round(island_core[1] - 50); } island_core[0] = (Math.random() * 100); water_top_full = true; console.log("Assigned sector top"); //return island_core; } else if (water_top_full == true){ island_core = assign_island_sector(); } } else if ((island_sector_distributor > (1/3)) && (island_sector_distributor <= (2/3))){ if (water_mid_full == false){ island_core[1] = (100 + (Math.random() * 100)); //Upper/lower boundaries for island core expectation //Change higher or lower based on how close islands get to the shore var core_upper_limit = Math.round(island_core[1] + 50); var core_lower_limit = Math.round(island_core[1] - 50); island_core[0] = (Math.random() * 100); water_mid_full = true; console.log("Assigned sector mid"); //return island_core; } else if (water_mid_full == true){ island_core = assign_island_sector(); } } else if (island_sector_distributor > (2/3)){ if (water_bottom_full == false){ island_core[1] = (200 + (Math.random() * 100)); core_upper_limit = Math.round(island_core[1] + 50); var core_lower_limit = Math.round(island_core[1] - 50); island_core[0] = (Math.random() * 100); water_bottom_full = true; console.log("Assigned sector bottom"); //return island_core; } else if (water_bottom_full == true){ island_core = assign_island_sector(); } } console.log("Island core: " + island_core[0] + ", " + island_core[1]); return island_core; } for (var i = 0; i < 3; i++){ console.log("ASSIGNING ISLAND " + (i + 1)); var island_center = assign_island_sector(); for (var p = 0; p < 2; p++){ console.log(island_center[p]); } }包含数字(可能包含小数),括号和任何数量的空格,我需要遍历String并处理每个数字和括号。

以下适用于String String,但如果我删除括号和数字"1 ( 2 3 ) 4"之间的空格,则无效。

"1 (2 3) 4)"

2 个答案:

答案 0 :(得分:1)

扫描程序使用空格作为默认分隔符。您可以将其更改为使用其他正则表达式模式,例如:

(?:\\s+)|(?<=[()])|(?=[()])

此模式将分隔符设置为左括号或右括号或一个或多个空格字符。但是,它还会保留左右括号(因为我认为你想在解析中包含那些?)而不是空格。

以下是使用此示例:

String test = "123(3 4)56(7)";

Scanner scanner = new Scanner(test);
scanner.useDelimiter("(?:\\s+)|(?<=[()])|(?=[()])");
while(scanner.hasNext()) {
    System.out.println(scanner.next());
}

输出:

123
(
3
4
)
56
(
7
)

详细的正则表达式说明:

(?:\\s+)|(?<=[()])|(?=[()])
    1st Alternative: (?:\\s+)
        (?:\\s+) Non-capturing group
            \\s+ match any white space character [\r\n\t\f ]
            Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    2nd Alternative: (?<=[()])
        (?<=[()]) Positive Lookbehind - Assert that the regex below can be matched
            [()] match a single character present in the list below
                () a single character in the list () literally
    3rd Alternative: (?=[()])
        (?=[()]) Positive Lookahead - Assert that the regex below can be matched
            [()] match a single character present in the list below
                () a single character in the list () literally

答案 1 :(得分:0)

扫描程序的.next()方法使用空格作为分隔符。幸运的是,我们可以更改分隔符!

例如,如果您需要扫描程序处理以处理空格和括号,则可以在构建扫描程序后立即运行此代码:

scanner.useDelimiter(" ()");