1

Hope the sed or awk gurus can help me out. I need to match a multiple line pattern in which the pattern range can span from one to three lines. I am not able to arrive at the result using pcregrep! The pattern I want to realise goes somewhat like this =>

\s*\w+\s\w+\([A-Za-z,]*\)\s*\;\{

The thing is that after the words or the semicolon, the following part of the pattern can or cannot occur in the next line AND the pattern can occur in a single line too, depending on the file.

Thanks in advance for the replies. I have been stuck at this step for almost a week! Any kind of suggestions appreciated :)

Eg : apple orange(plum); kiwi {

OR

apple orange(plum);

kiwi

{

An expression to match both expressions.

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
Gil
  • 1,518
  • 4
  • 16
  • 32
  • What are you trying to match? – David B Jun 18 '12 at 15:09
  • The syntax :[ word aphanumeric(Something);alphanumeric { ] The pattern doesnt necceserily occur in the same line but respects the specified syntax sequence. Thanks David for you reply. – Gil Jun 18 '12 at 15:19
  • It would be really helpful if you could post a sample of what you're matching against, and what you hope to extract out of it / match out of it. For example: `apple, I want to match all the p's`. – David B Jun 18 '12 at 15:20
  • I have to extract the string : – Gil Jun 18 '12 at 15:22
  • David,I have edited the question with an example :) Hope you have got my explanation .I was not always good at explaining things :( – Gil Jun 18 '12 at 15:28

1 Answers1

2

\s*(\w+)\s+(\w+?)\((\w+)\);\s*(\w+)\s*{

Yours was pretty close. Make sure you set the MULTILINE flag when you use the regex to ensure it matches across multiple lines. This will match all four words in your string. As per your example,

group 1: apple

group 2: orange

group 3: plum

group 4: kiwi

You can play with the regex here.

David B
  • 2,688
  • 18
  • 25