Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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?

+4
−1

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)?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+0
−2

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}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
General comments
Martin Bonner‭ wrote almost 4 years ago · edited almost 4 years ago

Will also match x1=y2; x1=y2; (which is I suspect why it is being downvoted).

Peter Taylor‭ wrote almost 4 years ago

@Martin, I know: that was the assumption which seemed reasonable, and I don't think that "requiring that both are present" was in the question when I wrote the answer - but I can't check, because the history has gone missing.