删除AS3中特定单词(字符串)之后的所有内容

时间:2018-07-24 06:17:59

标签: regex string actionscript-3

我有这个字符串,我想通过删除一个特定单词之后和另一个特定单词开始时的所有内容将其分成多个字符串。

示例文字:

“今天的预报是这个。今天早上:到处都是阳光。风很大,但没有危险。风速为30 knt。今天下午:云将遮住阳光。可能会下雨下午,但不要太多。今天晚上:下雨了。这将是满月。明天早晨:blblablb“

我想为每个“部分”创建多个字符串。早上弦乐节,下午弦乐节,夜晚弦乐节..等

如果我们以我的早晨字符串为例:如何删除“今天上午”之前的所有内容以及“今天下午”中的所有内容(为了只删除我的早晨字符串中的内容:“今天早晨:太阳会很宽裕到处都是。”

所以,如果我是对的,我已经做到了,可以从“今天上午”中进行选择:

var str: String = "Today forecast is this one. This morning : the sun will be generous everywhere. This afternoon : clouds will hide the sun. This night : rain will fall. Tomorrow morning : blblablblabla";  

var search_morning_starts: Number = str.indexOf("This");  
var morning_str:String = str.substring(search_morning_starts,str.length);  
trace(morning_str);  

但是如何添加“今天下午”中的所有内容?

(不能说“删除“所有地方”之后的所有内容,因为单词” everywhere”不会每次都写。这取决于早晨的天气。因此,当“今天下午”出现时,我需要删除所有内容)

编辑

一个预报的“天”中可以有多个句子

编辑2

我从未使用过Regex。如果有人能给我一个例子,说明如何在文本中选择一个部分,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

If the forecast will always have the same structure ("This morning : ... This afternoon : ... This night : ... Tomorrow morning : ..."), you can safely use those phrases to double split sections from it.

I'm not saying this is the best or most efficient way to do it, but it should work if your forecasts has that consistency.

var str:String = "Today forecast is this one. This morning : the sun will be generous everywhere. Wind will be strong but nothing dangerous. Wind speed will be 30 knt. This afternoon : clouds will hide the sun. It may rain during the afternoon but not too much. This night : rain will fall. It will be a full moon. Tomorrow morning : blblablb";

var separators:Array = new Array("This morning :","This afternoon :","This night :","Tomorrow morning :");

function separate(forecast:String):Object{
    var obj:Object = new Object();
    obj.pre = forecast.split(separators[0])[0];
    obj.morning = forecast.split(separators[0])[1].split(separators[1])[0];
    obj.afternoon = forecast.split(separators[1])[1].split(separators[2])[0];
    obj.night = forecast.split(separators[2])[1].split(separators[3])[0];
    obj.tomorrow = forecast.split(separators[3])[1];
    return obj;
}
var forecastObj:Object = separate(str);

trace(forecastObj.pre);
trace("This morning :"+forecastObj.morning);
trace("This afternoon :"+forecastObj.afternoon);
trace("This night :"+forecastObj.night);
trace("Tomorrow morning :"+forecastObj.tomorrow);