正则表达式-在比赛之前捕获单词,然后在比赛之后捕获单词 [英] Regex - capture word(s) before and then capture word after the match

查看:91
本文介绍了正则表达式-在比赛之前捕获单词,然后在比赛之后捕获单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎应该很简单,但是我花了4个小时阅读Espresso教程并尝试各种尝试,但都没有成功.正则表达式似乎只是一种我不懂的语言

需要记录大量的SQL Select语句,其中要选择一个系统中的数据并将其插入另一个系统中.在许多情况下,行的形式为

将columnX用作columnY

而在其他情况下,它们是

当...作为Z列时的情况

因此,"AS"一词在各行中很常见,我需要通过分别捕获 AS之前的 AS

It seems as if it ought to be simple, but I''ve spent 4 hours reading the Espresso tutorial and trying various expresions without success. Regex just seems to be a language I don''t understand

Need to document a LARGE number of SQL Select statements where data from one system is being SELECTed and INSERTed into another. In many cases the lines are of the form

columnX as columnY

and in other cases they are

CASE WHEN...as columnZ

So, the word "AS" is common in the lines and I need to show the map columnX to columnY or [expression] to columnZ by separately capturing the word(s) before the AS and the word after the AS

推荐答案

之后:(.*)(?:\ bas \ b)(.*)
Figured it out: (.*)(?:\bas\b)(.*)


正则表达式很性感.这也是时间上的麻烦,因为您可以将大量时间投入到其他琐碎的任务中.我迷失了尝试在正则表达式中编写一些非常漂亮的东西.

就是说,为什么不试试这个呢?将文件的所有行读入字符串数组,然后执行以下操作:

Regex is sexy. It''s also a time-pig as you can get to invest a lot of time in an otherwise trivial task. I''ve gotten lost trying to code some really beautiful things in regex.

That said, why not try this? Read all lines of the file into a string array, then do something like this:

string[] lines = File.ReadAllLines("yourfile");
var mappingLines = from l in lines
                   where l.Contains(" as ")
                   select l;
foreach (var line in mappingLines)
{
    string[] map = line.Split(" as ".ToCharArray());
}



第一位过滤掉您仅包含其中带有"as"的行,从而为您提供地图.第二位只是简单的字符串拆分.

拆分后,您可能需要在地图上进行一些操作以拉出CASE WHEN,但这将使您入门.我需要有关该文本模式的更多信息,以提供更多帮助.

干杯.



The first bit filters you down to only the lines with " as " in them, giving you your maps. The second bit is just a simple string split.

You might need to work a little on the map after the split to pull out the CASE WHEN, but that would get you started. I''d need more info on that text pattern to help more.

Cheers.


这篇关于正则表达式-在比赛之前捕获单词,然后在比赛之后捕获单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆