Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How can I write an egrep (grep -E) regexp that matches lines containing two stanzas in arbitrary order?
Parent
How can I write an egrep (grep -E) regexp that matches lines containing two stanzas in arbitrary order?
I have line-based data on the form
x1=y2; a3=b4; c5=d6; ...
Matching this with a extended regular expression is fairly straightforward; for example, one can do something not entirely dissimilar from
^([^;]+; )*x1=y2; ([^;]+; )*c5=d6;
to match the x1=y2
stanza anywhere within the input, and the c5=d6
stanza also occuring after the x1=y2
stanza.
However, the syntax for this data allows the tuples to be listed in any order, so it's just as valid to have as input
a3=b4; c5=d6; x1=y2; ...
How to write an extended regular expression that will match either of these inputs, while requiring that both x1=y2
and c5=d6
stanzas are specified (with those respective values)? Can that even be done without having to repeat either or resorting to more advanced processing than pure regular expressions (such as, for example, awk or Perl)?
Post
This doesn't answer the question in full generality, but the assumption made seems reasonable to me: match lines containing (x1=y2;|c5=c6;)
twice. I.e.
^(([^;]+; )*(x1=y2;|c5=c6;) ?){2}
0 comment threads