How do I use regexp to break a string based on the first set of parentheses?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Genevieve Grivas
le 3 Déc 2019
Commenté : Guillaume
le 3 Déc 2019
Hello,
I am trying to break a series of strings containing similar patterns into two parts.
Example of the string: "@(a23bcd)e5./(f+g)"
I would like the first part to be what appears inside the first set of parenthesis after the @ sign "a23bcd" and the second part to be everything afterwards "e5./(f+g)".
Currently I am using:
regexp(str,'@\((.*)\)(.*)','tokens','once')
however it gives me everything from the very first parentheses to the very last. "a23bcd)ef./(f+g"
What expression do I need to get everything within the first instance of parentheses followed by what ever appears afterward (including parentheses)?
Thanks
0 commentaires
Réponse acceptée
JESUS DAVID ARIZA ROYETH
le 3 Déc 2019
solution:
str="@(a23bcd)e5./(f+g)";
answer=regexp(str,'@\(([^\)]+)\)([^\n]+)','tokens');
firstpart=answer{1}{1}
secondpart=answer{1}{2}
1 commentaire
Guillaume
le 3 Déc 2019
An alternative would be to make the * in the original expression non-greedy:
regexp(str,'@\((.*?)\)(.*?)','tokens','once')
but Jesus' expression is probably more efficient (with the added 'once' option). For the record the proper equivalent would be:
regexp(str,'@\(([^\)]*)\)(.*)','tokens','once')
If there's a worry that . would match a newline, then 'dotexceptnewline' can be added to the regexp options. Matlab is the only language I know whose regular expression matches . to newlines by default.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!