使用LinuxBash-Delphi代码将字符串解析为bash代码
有人能告诉我如何在LinuxBash脚本中编写以下代码吗使用LinuxBash-Delphi代码将字符串解析为bash代码,linux,delphi,bash,Linux,Delphi,Bash,有人能告诉我如何在LinuxBash脚本中编写以下代码吗 procedure ParseLine(Line: String; var url, lang, Identifier: String); var p1,p2: Integer; Begin p1 := Pos(Char(VK_TAB),Line); p2 := PosEx(Char(VK_TAB),Line,p1+1); url := Copy(Line,1,p1-1); lang := Copy(Line,p1+1
procedure ParseLine(Line: String; var url, lang, Identifier: String);
var
p1,p2: Integer;
Begin
p1 := Pos(Char(VK_TAB),Line);
p2 := PosEx(Char(VK_TAB),Line,p1+1);
url := Copy(Line,1,p1-1);
lang := Copy(Line,p1+1,p2 - (p1+1));
Identifier := Copy(Line,p2+1,Length(Line));
p1 := Pos('(',lang);
lang := Copy(lang,1,p1-1);
End;
我需要解析的行看起来像这样
XXXXX\tab XXXX(XXX)\tab XXXX
谢谢。这是一个适用于示例输入的BASH脚本。不幸的是,我没有找到单独指定“Tab”字符的方法,我使用了
[:blank::
类(它还包括空格)。如果您真的只需要匹配制表符而不需要空格作为分隔符,您可以将所有出现的[:blank://code>替换为您从键盘键入的实际制表符。我也没有将匹配的部分保存到一些全局变量中(bash函数通常会这样做),我只是回显它们
#!/bin/bash
function split {
# Preapre small parts of the future regex. Makes writing the actual regex
# easier and provides a place to explain the regex
blank="[[:blank:]]" # one blank character (tab or space). Uses the [:blank:] character class in a character set regex selector
optional_blanks="${blank}*" # zero or more blank characters.
mandatory_blanks="${blank}+" # one or more blank characters.
non_blank="[^()[:blank:]]" # one character that is not tab space or paranthesis: This is the stuff we intend to capture.
capture="(${non_blank}+)" # one or more non-blank non paranthesis characters in captaruing paranthesis.
# Concatenate our regex building blocks into a big regex. Notice how I'm using ${optional_blanks} for maximum flexibility,
# for example around the "(" and ")" tests.
regex="${optional_blanks}${capture}${mandatory_blanks}${capture}${optional_blanks}\(${optional_blanks}${capture}${optional_blanks}\)${optional_blanks}${capture}${optional_blanks}"
# The regex is applied using the =~ binary operator.
if [[ $1 =~ $regex ]];
then
# We got a match, our capturing groups are saved into bash
# variables ${BASH_REMATCH[n]}. We'll echo those, but in
# real use the function would probably copy those values to
# some global names to be easily used from outside the function.
echo ${BASH_REMATCH[1]}
echo ${BASH_REMATCH[2]}
echo ${BASH_REMATCH[3]}
echo ${BASH_REMATCH[4]}
else
# Oops, input doesn't match.
echo not matched
fi
}
# call our function with static input for testing
# purposes.
echo "Test 1 - tab separated fields without extra space"
split "1234 56(78) 90"
# Since we're using [:blank:] and that includes both space and tab
# this also works
echo "Test 2 - space separated fields with lots of meaningless space"
split "1234 56 ( 78 ) 90 "
这是一个bash问题,而不是Delphi问题。它看起来像一个非常简单的正则表达式解压程序。你和巴什结婚了吗?我会自己用perl来做。可能是重复的谢谢,我也在摆弄正则表达式,但我似乎无法得到它的正确模式。类似这样的事情,取决于你的正则表达式味道(.*)\t(.*)\(.*))\t(.*)(.*)(.*)
不要关闭:我发现的重复表达式处理分隔符上的拆分,并且不使用正则表达式;OP特别要求使用正则表达式进行拆分,拆分不是简单的基于分隔符的拆分,因为输入的第三部分周围有括号。感谢Cosmin Prund,我将尝试并研究此脚本,以便理解:)