正規表現: 一致する文字列の一部を取得できません一致する文字列のすべての部分を抽出しようとしていますが、正規表現一致を使用しているため、必要な「BWA005、BWAZ006」を取得できません。
ex="Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
Regex match( ex, "[\!"](BWA|BWAZ|BWAZ_)([0-9]{1,})(.*?)[\!"]" )
自動化とスクリプト作成マックOSRe: 正規表現: 一致する文字列の一部を取得できませんこれが最良の解決策ではないことは確かですが、Regex Match() は、Python の re.findall のように、私が望んでいることを実行しないようです。この粗雑なスクリプトを使用して、すべての一致のリストを取得できます。誰かがより良い方法を知っていることを願っています。
Names Default To Here( 1 );
ex = "Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
matchlist = {};
While( !Is Missing( Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" ) ),
a = Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" );
Insert Into( matchlist, a );
Substitute Into( ex, a, "" );
);
Re: 正規表現: 一致する文字列の一部を取得できません実際、{ "BWA005", "BWAZ006", "BWA005" } のように、確認したい重複がそこにある場合は、代わりに次の方法を実行するとよいでしょう。
Names Default To Here( 1 );
ex = "Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
matchlist = {};
While( !Is Missing( Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" ) ),
a = Regex( ex, "(BWA|BWAZ|BWAZ_)(\d{1,})" );
Insert Into( matchlist, a );
b = Contains( ex, a );
c = Length( a );
ex = Substr( ex, b + c );
);Re: 正規表現: 一致する文字列の一部を取得できませんありがとう !それは動作します!
これを行うより良い方法があるかどうか知りたいです。
Re: 正規表現: 一致する文字列の一部を取得できません将来の JSL のメンテナにとっては良いでしょうか、それともコンピュータにとって、あるいはその他の対策にとって良いのでしょうか?ここでいくつかのアイデアを紹介します。私が最初のものに傾いているのは、テキスト文字列ではなく JSL 式から始めたと想像しているからでもあります。
コメントとツリーの表示とフォレストの表示に関するメモ: 私のコメントはツリーレベルです。それらはあなたの目標(森)を説明するのではなく、木々を説明します。将来的に JSL を保守する人は両方の種類を必要とするでしょう。フォレスト レベルのコメントでは、これらの値について考える理由を説明する必要があります。ツリー レベルは、明白ではないコード部分がどのように機能するかに関するものです。
ex = "Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
// write(ex) // Select Where( :"BWA"n == "BWA005" | :"BWA"n == "BWAZ006" )
// Regex match( ex, "[\!"](BWA|BWAZ|BWAZ_)([0-9]{1,})(.*?)[\!"]" )
// desired result: "BWA005,BWAZ006"
//
// it isn't 100% clear what the rules are. These examples make different assumptions.
// assume the text is valid JSL that can be parsed back into an expression
// below, also assumes | but not & and no nested parens
expression = Parse( ex ); // Select Where( :BWA == "BWA005" | :BWA == "BWAZ006" )
// get the argument of the select where(...)
expression = Arg( expression, 1 ); // :BWA == "BWA005" | :BWA == "BWAZ006"
// assume there are only | (or) operators, change x|y|z => {x,y,z}
Substitute Into( expression, Expr( Or() ), {} ); // {:BWA == "BWA005", :BWA == "BWAZ006"}
result = {}; // accumulate answer in a result list
// process the expression list
For Each( {op}, expression, // :BWA == "BWA005" etc.
// the Right Hand Side of :BWA == "BWA005" is "BWA005"
RHS = Arg( Name Expr( op ), 2 ); // assign(:BWA,"BWA005"), want 2nd arg
// apply the test to see if this is one to keep
If( !Is Missing( Regex( RHS, "(BWA|BWAZ|BWAZ_)([0-9]+)" ) ),
Insert Into( result, Arg( op, 2 ) ); // yes: keep it in the result list
);
);
// join the list of RHS strings, separated by commas
Show( Concat Items( result, "," ) ); // Concat Items(result, ",") = "BWA005,BWAZ006";
<BR />// alternate example<BR /><BR />
// assume the strings can't collide with column names, perhaps because no
// column name has a suffix number and all strings do have a suffix number
result = {};
Pat Match(
ex,
// this is the pattern; it repeats as long as it can
Pat Repeat(
// either match the BWA... pattern, in quotes, stashing the regex match into the result:
( "\!"" + Pat Regex( "(BWA|BWAZ|BWAZ_)([0-9]+)" ) >> result[N Items( result ) + 1] + "\!"" )
// >> is the patImmediate() operator that copies the LHS match into the RHS location
// the list is initially 0 items long, and [nitems+1] extends the list by one more item
| // OR the alternative...
Pat Len( 1 ) // advance one character. This is how most of the text is matched.
)
);
Show( Concat Items( result, "," ) ); // Concat Items(result, ",") = "BWA005,BWAZ006";
// you *could* write a pattern match that would parse the text as an expression,
// but that is what the first example did.Re: 正規表現: 一致する文字列の一部を取得できませんRegex Match には願いがあります
Regex Match() にフラグを追加して、重複しない pattern の出現をすべて検索します。何をしようとしているか (+ なぜ、どこで) に応じて、これを処理するさまざまなオプションが多数あります (いくつか例を挙げると、すでに述べたように、ループや jmp 独自のパターン マッチングなど) ですが、ユースケースにより適したオプションが他にもある可能性があります。
Re: 正規表現: 一致する文字列の一部を取得できないJmp18 では、Python の re.findall を使用するだけで、作業がはるかに簡単になりました。
ex="Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
Python send(ex);
Python Submit ("
import re
matches = re.findall(r'(BWA|BWAZ)([0-9]+)',ex)
");
Python get (matches)