将空格分隔列表转换为 SQL where 子句的正则表达式

发布于 2024-08-19 16:51:28 字数 837 浏览 2 评论 0原文

我几乎感到尴尬,但我正在努力创建一个正则表达式来将 catdog mouse 之类的内容更改为 SQL where 子句:

a.cat=b.cat AND a.dog=b.dog AND a.mouse=b.mouse

有了

s/(\w*)\s?/a.$1=b.$1 AND /

我就

a.cat=b.cat AND a.dog=b.dog AND a.mouse=b.mouse AND a.=b. AND

哎哟。帮助表示赞赏。

编辑:我最终使用了两个连续的正则表达式。由于我在 SAS 宏中需要这个并且我希望我的代码简洁,所以我编写了这个宏:

%Macro rxchange(str,rx1,rx2,rx3,rx4,rx6,rx7,rx8,rx9,rx10);
    %Let rxno=1;
    %Do %While("&&rx&rxno" Ne "");
        %Let str=%SysFunc(PRXChange(%Str(&&rx&rxno), -1, %Str(&str)));        
        %Let rxno=%Eval(&rxno+1);
    %End;
    &str
%Mend;

/* Try this: */
%Put %rxchange(cat dog mouse,s/(\w+)\s?/a.$1=b.$1 /,s/(\s+)/ AND /);

感谢所有回复的人!

I'm almost embarassed, but I'm struggling to create a regular expression to change something like cat dog mouse to a SQL where clause:

a.cat=b.cat AND a.dog=b.dog AND a.mouse=b.mouse

With

s/(\w*)\s?/a.$1=b.$1 AND /

I get

a.cat=b.cat AND a.dog=b.dog AND a.mouse=b.mouse AND a.=b. AND

Ouch. Help appreciated.

EDIT: I ended up using two consecutive regexes. Since I needed this in a SAS macro and I wanted my code to be concise, I wrote this macro:

%Macro rxchange(str,rx1,rx2,rx3,rx4,rx6,rx7,rx8,rx9,rx10);
    %Let rxno=1;
    %Do %While("&&rx&rxno" Ne "");
        %Let str=%SysFunc(PRXChange(%Str(&&rx&rxno), -1, %Str(&str)));        
        %Let rxno=%Eval(&rxno+1);
    %End;
    &str
%Mend;

/* Try this: */
%Put %rxchange(cat dog mouse,s/(\w+)\s?/a.$1=b.$1 /,s/(\s+)/ AND /);

Thanks for all who replied!

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

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

发布评论

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

评论(4

静谧 2024-08-26 16:51:28

您的第一个问题是您需要 + 而不是 *

s/(\w+)\s?/a.$1=b.$1 AND /

这将解决 a.=b. 的问题。

即使这样,你也会得到太多的AND。你可以在最后用“1=1”来解决这个问题。

您确定要在这里使用正则表达式,而不是拆分、简单的字符串操作,然后进行连接吗?我想这样会更容易理解。

Your first problem is you need + instead of *:

s/(\w+)\s?/a.$1=b.$1 AND /

This will solve the problem with a.=b..

Even then you will get an AND too much. You could solve this with a '1=1' at the end.

Are you sure you want to use regex here, and not a split, simple string manipulation, then a join? I think this would be simpler to understand.

拒绝两难 2024-08-26 16:51:28

您可以按空格分割行,执行s/^(.+)$/a.$1=b.$1/(对于这种简单的情况,不要使用正则表达式不过),然后使用“分隔符”“AND连接数组。

确保在开始之前修剪字符串。结尾空格是额外 a.=b. 的原因。

You can split the line by spaces, perform s/^(.+)$/a.$1=b.$1/ (don't use regex for this simple situation though), then join the array with the "separator" "AND".

Make sure you trim the string before you start. The ending space is the reason for the extra a.=b..

熟人话多 2024-08-26 16:51:28

您可以使用两个正则表达式来完成此操作,一个匹配除第一个单词之外的所有内容,另一个匹配第一个单词。

C#代码:

string where = "cat dog mouse";
where = Regex.Replace(where, @" (\w+)", " AND a.$1=b.$1");
where = Regex.Replace(where, @"^(\w+)", "a.$1=b.$1");

You can do it with two regular expressions, one that matches everything but the first word, and one that matches the first.

C# code:

string where = "cat dog mouse";
where = Regex.Replace(where, @" (\w+)", " AND a.$1=b.$1");
where = Regex.Replace(where, @"^(\w+)", "a.$1=b.$1");
清泪尽 2024-08-26 16:51:28

我可以用两个正则表达式来命名那首曲子!

str = prxchange('s/(\w+)/a.$1=b.$1/', -1, str);
str = prxchange('s/ +/ AND /', -1, str);

I can name that tune in 2 regular expressions!

str = prxchange('s/(\w+)/a.$1=b.$1/', -1, str);
str = prxchange('s/ +/ AND /', -1, str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文