I would like to create a regex for the following.
I have some text like the following:
field = "test string";
type = INT;
funcCall(.., field, ...);
...
text = "desc";
field = "test string 1";
type = FLOAT;
funcCall(.., field, ...);
...
text = "desc 2";
field = "test string 2";
type = FLOAT;
funcCall(.., field, ...);
...
text = "desc 3";
.... keeps repeating
Basically I'm trying to create a regex that would get all text from the start of the first "field =" to the start of the second "field = ". It has to skip past the field text used in the function call.
I currently have the following:
my @overall = ($string =~ m/field\s*=.*?/gis);
However, this just obtains the text "field = ". Without the "?" it gets all the data from the first all the way to the very last instance.
I also tried:
my @overall = ($string =~ m/field\s*=.*field\s*=/gis);
However, that will then get me every other instance since it is possessive of the second "field =" string. Any suggestions?
This is hard for a regex. Fortunately, that isn't the only tool in your box.
It looks like you have a blank line between each record. If so, you can do this easily by setting
$/to"\n\n". Then you can read your file with a while loop, and each iteration$_will be set to the chunk you are trying to handle.Failing that, you could set it to
field =or perhaps even just usesplit