Regex: impossibile ottenere tutte le parti della stringa che corrispondonoSto cercando di estrarre tutte le parti della stringa che corrispondono, ma sto utilizzando la corrispondenza regex e non riesco a ottenere il "BWA005, BWAZ006" che desidero.
ex="Select Where( :\!"BWA\!"n == \!"BWA005\!" | :\!"BWA\!"n == \!"BWAZ006\!" )";
Regex match( ex, "[\!"](BWA|BWAZ|BWAZ_)([0-9]{1,})(.*?)[\!"]" )
Automazione e scriptingMac OSRe: Regex: impossibile ottenere tutte le parti della stringa che corrispondonoSono sicuro che questa non sia la soluzione migliore, ma Regex Match() non sembra fare quello che vorrei, come re.findall di Python. Puoi ottenere un elenco di tutte le corrispondenze con questo script grezzo. Si spera che qualcuno conosca un modo migliore.
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: Regex: impossibile ottenere tutte le parti della stringa che corrispondonoIn realtà, nel caso in cui siano presenti duplicati che desideri vedere, come { "BWA005", "BWAZ006", "BWA005" }, dovresti invece farlo in questo modo:
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: Regex: impossibile ottenere tutte le parti della stringa che corrispondonoGrazie ! Funziona!
Voglio sapere se esiste un modo migliore per farlo.
Re: Regex: impossibile ottenere tutte le parti della stringa che corrispondonoMeglio per un futuro manutentore del JSL, o meglio per il computer, o qualche altra misura? Ecco un paio di idee; Io propendo per la prima, in parte perché immagino che tu abbia iniziato con un'espressione JSL, non con una stringa di testo.
Una nota sui commenti e sul vedere gli alberi rispetto al vedere le foreste: i miei commenti sono a livello di albero. Non descrivono il tuo obiettivo (la foresta), descrivono gli alberi. Chiunque mantenga il tuo JSL in futuro vorrà entrambi i tipi. Un commento a livello di foresta dovrebbe spiegare perché stai pensando a questi valori. Il livello dell'albero riguarda il funzionamento di una parte di codice non ovvia.
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: impossibile ottenere tutte le parti della stringa che corrispondonoPer Regex Match c'è desiderio
Aggiungi flag a Regex Match() per trovare tutte le occorrenze non sovrapposte di pattern . A seconda di cosa stai cercando di fare (+ perché e dove) ci sono molte opzioni diverse per gestirlo (per citarne alcune già menzionate: loop e pattern match di jmp) ma potrebbero esserci più opzioni più adatte al tuo caso d'uso.
Re: Regex: impossibile ottenere tutte le parti della stringa che corrispondonoCon Jmp18, è diventato molto più semplice usare re.findall di Python:
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)