用红宝石中的正则表达式替换句子中的单词

时间:2017-10-13 01:09:24

标签: ruby regex

如何使用Ruby regex将Markdown格式替换为HTML:

hallo _html_ dan *gaga* sas *tes*

hallo <em>html</em> dan <strong>gaga</strong> sas <strong>tes</strong>

1 个答案:

答案 0 :(得分:1)

已经有了所有的宝石,例如redcarpet

这里是我使用默认降价渲染器的示例,但如果您需要不同的语法,则可以自己制作渲染器。

#include <iostream>
using namespace std;

int main()
{
    bool run = true;
    while (run) {
        double len, width, area, volume, height;
        char check;
        cout << "Please, enter length,width and height of a prism(rectangular)!";
        cin >> len >> width >> height;
        area = width*len;
        volume = area*height;
        cout << "The area of rectangle is equal to: " << area << "\n" << "The volume of rectangular prism is equal to: " << volume << "\n";
        do {
            cout << "Do you want to try again?(y/n)\n"; cin >> check;
            cin.clear();
        } while (check != 'y' && check !='n');

        if (check == 'n') {
            run = false;
        }
    }
    return 0;
}

一般来说,使用带有html的正则表达式或重新使用轮子是一个坏主意,但如果你坚持,这里有一个简单的例子,你可以做到这一点。

require 'redcarpet'
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown.render("**This** _is_ an [example](http://example.org/).")
# => "<p><strong>This</strong> <em>is</em> an <a href="http://example.org/">example</a>.</p>"
相关问题