Page 1 of 1

Delete part of a string

Posted: 08 May 2017 09:18
by Frejoh466
So I'm getting weather from an .xml file every morning and save it to a variabel. 3 exapemls of the text,

"Sun and clouds mixed. Slight chance of a rain shower. High 3C. Winds NNE at 25 to 40 km/h."
"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight. Low -10C. NNE winds shifting to WSW at 15 to 25 km/h. Chance of precip 50%."
"Showers early becoming a steady light rain later in the day. Snow may mix in. High 2C. Winds WNW at 10 to 15 km/h. Chance of rain 80%."


What I want to do is to remove the wind part, eg.

Winds NNE at 25 to 40 km/h
NNE winds shifting to WSW at 15 to 25 km/h
Winds WNW at 10 to 15 km/h


If I get the Regular expression syntax right, then I should be able to delete from "Wind" to "km/h" and then have something to delete the direction. At the moment I'm ruining this,

day=replaceAll("{day}", "(SSW )|(SW )|(W )|(NNW )|(NW )|(N )|(NNE )|(NE )|(E )|(Winds )|(shifting )|(at )|[0-9][0-9] to [0-9][0-9]|(km/h)", " ");

which is not optimal. I have been trying to find how to write it, but the syntax doesn't help much. (nor that the notification for nougat doesn't work, so I have to use tts to find what the string contain. I found no good option to find what the variabels contain except the global ones, but tts is faster than to check that every time.)

Re: Delete part of a string

Posted: 08 May 2017 12:32
by Bingwu
Hello Frejoh466!

Maybe this helps you. Unfortunately, I do not have so much experience.
It is based on this post http://automagic4android.com/forum/view ... lit#p17045

It looks for the punctuation mark "." and splits the string at this point.
If a part does not contain the word "Winds", it is considered.

Action -> Script:
day = "Sun and clouds mixed. Slight chance of a rain shower. High 3C. Winds NNE at 25 to 40 km/h.";
daytemp = "";
for(part in split(day,"\\."))
{
if(contains(part,"Winds") == false)
{
daytemp = daytemp + part + "."
}
}
day = daytemp
regards
Peter

Re: Delete part of a string

Posted: 08 May 2017 17:47
by Frejoh466
Interesting script. A few problems tho. It will delete everything after "Winds" right? so in

"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight. Low -10C. NNE winds shifting to WSW at 15 to 25 km/h. Chance of precip 50%."

It will delete "Chance of precip 50%.". It also doesn't work if it start with the direction, or if its capital letter or not. But I might be able think of something now that I know I don't have to use the replace command.

Probably could just do a break at km/h.

Re: Delete part of a string

Posted: 08 May 2017 18:34
by Bingwu
Hello Frejoh466! (Please excuse my bad English!)

The script looks for a point "\\." and separates the text into individual parts.

The original:
"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight. Low -10C. NNE winds shifting to WSW at 15 to 25 km/h. Chance of precip 50%."
The parts:
"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight"
" Low -10C"
" NNE winds shifting to WSW at 15 to 25 km/h"
" Chance of precip 50%"
The script then looks for the keyword (in this case "winds") or keywords:
keyword/s = false -> "Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight"
keyword/s = false -> " Low -10C"
keyword/s = true -> " NNE winds shifting to WSW at 15 to 25 km/h"
keyword/s = false -> " Chance of precip 50%"
After that, all parts that do not contain the keyword/s are put together (+ "."):
"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight" + "." + " Low -10C" + "." + " Chance of precip 50%" + "."
Result:
"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight. Low -10C. Chance of precip 50%."
You can still expand the keyword/s:
if(contains(part,"winds") == false and contains(part,"Winds") == false and contains(part,"precip") == false)
Result:
"Partly cloudy skies during the evening will give way to cloudy skies and light freezing rain after midnight. Low -10C."
regards
Peter

Re: Delete part of a string

Posted: 08 May 2017 19:11
by bogdyro
Try this pattern with replaceAll '(?i-s)\\.[\\w\\s ]*(?=winds).*/h'
It will replace every sentance that has the word winds in it.
It is dependent on a '.' character being the boundry of a sentance.

Re: Delete part of a string

Posted: 09 May 2017 08:20
by Frejoh466
@Bingwu

Ah, now I get it. You explained it fine, it's just me who are a little bit daft to understand the code. Thanks for the help.

@bogdyro
That would probably work too, with less code. Thanks.