0

I have a variable like so:

output = "KORD 142327Z 1500/1606 31012KT P6SM FEW060 SCT300 FM150200 04007KT >P6SM FEW300 FM150400 01005KT P6SM SKC FM151400 22008KT P6SM SCT250 FM151900 >22014G23KT P6SM BKN250 FM160200 23014KT P6SM SCT150 BKN250 WS010/25040"

I want to insert a newline before every "FM" character sequence so I can print it to user and its easier to read.

Currently my code is as follows, but replaces the first "FM" sequence with a "\n" then stops without continuing onto the next "FM" set.

output [/(FM{1})/, 0] = "\n"

I don't want to replace FM with newline. I need to append "\n" immediately before each "FM" character set. Any help with this would be appreciated.

1 Answer 1

1

Try this:

 output.gsub!("FM","\nFM")
Sign up to request clarification or add additional context in comments.

4 Comments

Make that output.gsub!("FM","\nFM")
that would replace FM with a new line. I don't want to replace FM. Just put a new line before every occurrence of FM
If "FM" is only to be converted to "\nFM" when "F" is immediately preceded by a word boundary, change "FM" to /\bFM/. That will prevent, say, "'IFM'" from being converted to "I\nFM".
Thanks Cary. When you say word Boundary, do you mean a character? Having characters prior to "F" is not my situation, but I will keep this in mind if a similar situation comes up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.