匹配字符串,需要特定的字符

时间:2019-02-18 13:46:38

标签: regex bash perl

所以我现在已经尝试了一段时间,以匹配字符串然后从中排除某个字符。

示例:

INSERT INTO public.tags (created_at,

我想结束:

INSERT INTO tags (created_at,
  • 我想罢免公众。 -但可以是任何字符串,后跟一个点。

这就是我尝试过的没有运气的胖子

\.tags仅匹配“ .tags”

([^\s]+)(\.tags)与整个public.tags字符串匹配

3 个答案:

答案 0 :(得分:2)

s/\S+\.tags\b/tags/

s/\S+\.(?=tags\b)//

答案 1 :(得分:2)

您的问题不是很清楚。但是我想你是说你有:

INSERT INTO public.tags (created_at,

最后您要结束:

INSERT INTO tags (created_at,

您要删除public.-但可以是任何字符串,后跟一个点。

这似乎可以满足您的要求。

#!/usr/bin/perl

use strict;
use warnings;

my $sql = 'INSERT INTO public.tags (created_at,';

# Use s/.../.../ - the substitution operator.
# \S - match any non-whitespace character.
# + - match one or more of the previous atom.
# \. - match a dot.
# tags - match the string 'tags'
# \b - match the end of a word
# So match a string of non-whitespace characters,
# followed by '.tags' at the end of a word.
# Replace it with 'tags'.
$sql =~ s/\S+\.tags\b/tags/;

print $sql;

答案 2 :(得分:0)

sed可以轻松做到这一点。

echo "INSERT INTO public.tags (created_at," | sed 's/tags//'

这是特定于您的示例的,但是我们可以根据需要轻松添加到该示例中。