Seems like a really simple question but it's driving me wild. The answers I've found are far too complex.
看起來像一個非常簡單的問題,但它讓我瘋狂。我發現的答案太復雜了。
I need to create a basic markdown script. I have a text file with this line:
我需要創建一個基本的降價腳本。我有一行文本文件:
# heading 1
I want to remove the "# " from the start so I use regex s/
我想從頭開始刪除“#”所以我使用正則表達式s /
$i =~ s/^#\s//;
Success! $i now reads
成功! $ i現在讀
heading 1
The next step is to add html tags to make it a header. I need $i to be:
下一步是添加html標簽以使其成為標題。我需要$ i:
<h1>heading 1</h1>
Seems so simple
似乎很簡單
$i = <h1>$i</h1>;
nope...perl gets crazy about the use of <>. OK so escape them with single quotes..
nope ... perl對使用<>感到瘋狂。好的,所以用單引號逃脫它們。
$i = '<h1>'$i'</h1>';
Still nope. And I've been changing things and trolling answers for hours now to the point I'm more confused than when I started. Can someone please explain it like I'm 5 how the hell to make $i the string I need it to be.
還是不高興。而且我現在一直在改變事物和拖曳幾個小時的答案,以至於我比起初時更加困惑。有人可以解釋一下,就像我5是如何讓我成為我需要它的字符串。
Thanks
謝謝
0
You either need to substitute inside a double-quoted string:
您需要在雙引號字符串中替換:
$i = "<h1>$i</h1>";
or use string concatenation:
或使用字符串連接:
$i = '<h1>' . $i . '</h1>';
2
You could achieve this with a single replacement.
你可以通過一次更換實現這一目標。
my $string = "# heading 1";
$string =~ s/^#\s*(.*)/<h1>$1<\/h1>/;
print $string."\n";
0
Of course, you could also automatically deduce the heading level using the number of leading octothorpes.
當然,您也可以使用前導octothorpes的數量自動推斷出標題級別。
#!/usr/bin/env perl
use strict;
use warnings;
while (my $line = <DATA>) {
next unless $line =~ /\A ([#]{1,6}) \s+ ([^\n]+) $/x;
my $level = length $1;
print "<h$level>$2</h$level>\n";
}
__DATA__
# heading 1
## heading 2
### heading 3
#### heading 4
##### heading 5
###### heading 6
Output:
輸出:
<h1>heading 1</h1>
<h2>heading 2</h2>
<h3>heading 3</h3>
<h4>heading 4</h4>
<h5>heading 5</h5>
<h6>heading 6</h6>
-1
Double quotes are working fine without error. I tried this with following example
雙引號工作正常,沒有錯誤。我用以下示例嘗試了這個
$i = "<h1>Here Comes The Header<\h1>";
print "$i";
output:
<h1>Here Comes Header<h1>
I think there will not be any problem with double quotes
我認為雙引號不會有任何問題
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/04/13/8b49c02c25e627fef2959c4cd1caa808.html。