linux - 使用 linux bash 解析字符串 - Delphi 代码到 Bash 代码

标签 linux delphi bash

有人可以告诉我如何在 Linux bash 脚本中编写以下代码吗?

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 脚本。不幸的是,我没有找到单独指定“制表符”字符的方法,我使用了 [:blank:] 类(它还包括空格)。如果您确实需要仅匹配制表符而不是空格作为分隔符,则可以将所有出现的 [:blank:] 替换为您从键盘输入的实际 TAB 字符。我也没有将匹配的部分保存到某些全局变量中(就像 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       "

关于linux - 使用 linux bash 解析字符串 - Delphi 代码到 Bash 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737606/

相关文章:

php - Python 一个打印命令两个变量输出两条不同的行到屏幕

linux - Github 不响应 SSH

linux - 定义带或不带导出的变量

linux - 在特定列中具有相同值的行的总值

java - tomcat占用空间多然后配置?

php - LAMP 服务器的 Linux 用户和组

delphi - Delphi Android无法使用TPath

arrays - 向多维数组中插入DBGrid数据

delphi - 实现 TObjectList<myClass> 的自定义二进制搜索 (Delphi XE)

python - 从 shell 和 python 中的行中删除字符串