开发者

RegEx to capture values in a string into a group, regardless of order?

I have a bunch of strings in a text file that have values in them I want to capture into groups. Each line/row in the text file is a unique record, but the values usually stick to a prescribed format in each line:

ValueA ValueD ValueS ValueR ValueW ValueT
ValueX ValueW ValueB ValueM ValueQ ValueA

Etc. If I want to capture ValueA into capture group 1 and ValueW into capture group 2, can I do so using a single regex that handles things properly irrespective of the position in each line of text of the values in question?

The goal is to then use the capture groups to write back out to a file the two captured values so that I can or开发者_如何学Cder them, i.e., "ValueW ValueA".


You could use something like:

^(?=.*?(ValueA))(?=.*?(ValueW)).*

Perl example:

$_ = <<'_STR_';
ValueA ValueD ValueS ValueR ValueW ValueT
ValueX ValueW ValueB ValueM ValueQ ValueA
_STR_

s/^(?=.*?(ValueA))(?=.*?(ValueW)).*/$1 $2/gm;

print;

Output:

ValueA ValueW
ValueA ValueW

Also at http://ideone.com/IhGfS

Note: If you are just matching you won't need that last .*, and depending on how well matching the ValueX patterns are you could remove all the ?.


Something like this maybe:

(?<ValueA>ValueA)|(?<ValueW>ValueW)


assuming its unix, first find and replace space with newline, pipe it with the various regex's,then output the groups to a file.


This is regex abuse - call the cops!

Seriously... if you HAVE to do this, you could use the or operator (|) in each group to select one or the other of the two words.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜