0

I have a text like

details = "1. Command id = 22 Time = 2:30 <br> 2. Command id = 23 Time = 3:30"

Now I need to convert it into

"1. http://localhost:3000/command_id=22/home Time = 2:30 <br> 2. http://localhost:3000/command_id=23/home Time = 3:30"

I used regex and gsub but it will not do that as gsub will replace same string. Then there are some techniques using sed and awk.

To extract all ids like 22, 23, I used

details.scan(/Command id = [0-9]+/).join(",").scan(/[0-9]+/)

Any ideas how to do the above conversion?

1
  • 1
    What are you actually trying to achieve? Removing Command id = from the string or extracting the ids or both? Commented Oct 6, 2015 at 7:47

6 Answers 6

2

Why dont you just use

details.gsub(' Command id =', '')

It generates expected result

"1. 22 Time = 2:30 <br> 2. 23 Time = 3:30"

EDIT:

details.gsub('Command id = ', 'http://localhost:8000/')

It will generate

 "1. http://localhost:8000/22 Time = 2:30 <br> 2. http://localhost:8000/23 Time = 3:30"
Sign up to request clarification or add additional context in comments.

1 Comment

I have edited my post.Can you please have a look into it.I want to generate http link .
2

Simple regex

details.gsub(' Command id =', '')

#=> "1. 22 Time = 2:30 <br> 2. 23 Time = 3:30"

Comments

2
string.gsub('Command id =', '')

Comments

1

Just replace Command id = with an empty string

string.gsub(/\s*\bCommand\s+id\s+=/, "")

1 Comment

You could just provide a string pattern, i.e. gsub('Command id = ', '')
0

Try this pure regex and get your expected output

sed 's/[^"]\+\([^ ]\+\)[^=]\+=\([^\.]\+.\)[^=]\+.\(.*\)/\1\2\3/' FileName

or

sed 's/Command id =\|details = //g' FileName

Output :

"1. 22 Time = 2:30 <br> 2. 23 Time = 3:30"

Comments

0
def parse_to_words(line)line.split ' '
end

line = "1. Command id = 22 Time = 2:30 <br> 2. Command id = 23 Time = 3:30"

words = parse_to_words line

output= words[0] +  
        " http://localhost:3000/command_id=" + 
        words[4] +
        "/home Time = " +
        words[7] + 
        " <br> " + 
        words[9] +
        " http://localhost:3000/command_id=" +
        words[13] + 
        "/home Time = " +
        words[16]

Output: 1. http://localhost:3000/command_id=22/home Time = 2:30 <br> 2. http://localhost:3000/command_id=23/home Time = 3:30

Can of course be automated further

Comments

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.