velpandian
velpandian

Reputation: 471

How to match the my expected output through regular expression using TCL

The below one is my real scenario:

I will have output as below:

Neighbour Group : Testing

Server Address - 10.0.0.1 Neighbour Group : Testing Date :Jul 08 18:26:32 2014 Queries Pending : 0/1
Requesting Method :Round-robin Server Address - 20.0.0.1 Neighbour Group : Testing Date :Jul 08 18:26:32 2014 Queries Pending : 0/1 Requesting Method :Round-robin

Server Address - 30.0.0.1 Neighbour Group : Testing Date :Jul 08 18:26:32 2014 Queries Pending : 0/1
Requesting Method :Round-robin

From the all above output I want to group the output in a TCL list,

I want to capture from (starting delimiter) where Server Address starts in the given string to the word before where next time Server Address coming.

So, for first server, regexp output should be like:

{Server Address - 10.0.0.1 Neighbour Group : Testing
Date :Jul 08 18:26:32 2014 Queries Pending :
0/1 Requesting Method :Round-robin}

So, for second server, regexp output should be like:

{Server Address - 20.0.0.1 Neighbour Group : Testing
Date :Jul 08 18:26:32 2014 Queries Pending :
0/1 Requesting Method :Round-robin}

And same, for third server, starting delimiter is Server Address then afterwards there is no another ending delimiter such as Server Address. So, in that case it should go and find up to last character of the given string.

for third server after regexp output should be like

{Server Address - 30.0.0.1 Neighbour Group : Testing
Date :Jul 08 18:26:32 2014 Queries Pending :
0/1 Requesting Method :Round-robin }

Upvotes: 1

Views: 267

Answers (2)

Donal Fellows
Donal Fellows

Reputation: 137567

With your revised question, you need a pretty horrible RE to get exactly what you want:

% regexp -all -inline {rose(?:\s*(?!rose)\S)*} $string
{rose is an flower} {rose is an flower} {rose is a flower} {rose is not a place}

If you don't mind some extra spaces, you can use this simpler one:

% regexp -all -inline {rose(?:(?!rose).)*} $string
{rose is an flower } {rose is an flower } {rose is a flower } {rose is not a place}

However, I think you are not telling us what the real thing you're doing is. Regular Expressions are very specific to what you're trying to do; generalizing them isn't trivial.

Upvotes: 1

nurdglaw
nurdglaw

Reputation: 2117

For the updated question, the pattern rose.*?flower seems to do the trick.

% set string {rose is an flower rose is an flower rose is a flower rose is a color and an flower}
% set pat rose.*?flower
% regexp -all -inline $pat $str
{rose is an flower} {rose is an flower} {rose is a flower} {rose is a color and an flower}

(Other command echo not quoted for clarity.)

I have a nasty suspicion that you actually want to do something more general, but I can only answer the questions that you pose.

Upvotes: 2

Related Questions