正则表达式删除不需要的文本

发布于 2024-09-18 05:35:28 字数 322 浏览 6 评论 0原文

总的来说,我对正则表达式还是比较陌生。我正在尝试从字段中检索名称,以便可以将它们拆分以供进一步使用(使用 Pentaho 数据集成/Kettle 进行数据提取)。下面是我给出的字符串的示例:

CN=Name One/OU=Site/O=Domain;CN=Name Two/OU=Site/O=Domain;CN=Name Three/OU=Site/O=Domain

我希望返回以下格式:

Name One;Name Two;Name Three

Kettle 使用 Java 正则表达式。

I'm still kind of new to RegEx in general. I'm trying to retrieve the names from a field so I can split them for further use (using Pentaho Data Integration/Kettle for the data extraction). Here's an example of the string I'm given:

CN=Name One/OU=Site/O=Domain;CN=Name Two/OU=Site/O=Domain;CN=Name Three/OU=Site/O=Domain

I would like to have the following format returned:

Name One;Name Two;Name Three

Kettle uses Java Regular Expressions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留蓝 2024-09-25 05:35:28

听起来您想要基于正则表达式进行替换和替换。如何正确执行此操作取决于您的语言。但是使用 sed 我会这样做:

echo "CN=Name One/OU=Site/O=Domain;CN=Name Two/OU=Site/O=Domain;CN=Name Three/OU=Site/O=Domain" |\
sed 's/CN=\([^\/]*\)[^;]*/\1/g'

如果您打算稍后分割它,您可能只想匹配名称并在循环中返回它们。 perl 中的示例代码:

#!/usr/bin/perl
$line="CN=Name One/OU=Site/O=Domain;CN=Name Two/OU=Site/O=Domain;CN=Name Three/OU=Site/O=Domain";
for $match ($line =~ /CN=([^\/]*)/g ){
  print "Name: $match\n";
}

That sounds like you want substitute&replace based on a regex. How to correctly do that depends on your language. But with sed I would do it like this:

echo "CN=Name One/OU=Site/O=Domain;CN=Name Two/OU=Site/O=Domain;CN=Name Three/OU=Site/O=Domain" |\
sed 's/CN=\([^\/]*\)[^;]*/\1/g'

If you intend to split it later anyway, you probably want to just match the names and return them im a loop. Example code in perl:

#!/usr/bin/perl
$line="CN=Name One/OU=Site/O=Domain;CN=Name Two/OU=Site/O=Domain;CN=Name Three/OU=Site/O=Domain";
for $match ($line =~ /CN=([^\/]*)/g ){
  print "Name: $match\n";
}
太阳男子 2024-09-25 05:35:28

假设你在 file.txt 中有它:

sed -e  's/\/OU=Site\/O=Domain//g' -e 's/CN=//g' file.txt

assuming you have it in file.txt:

sed -e  's/\/OU=Site\/O=Domain//g' -e 's/CN=//g' file.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文