Reputation: 45
Need help in creating Regex for below -
Input -
Steps_taken "" loop
{
_1 "1st"
[
Sub cat = "_1,_2,_3,_4,_5,_6,_7"
],
_2 "2nd"
[
Sub cat = "_1,_2,_3,_4,_5,_6,_7,_98"
]
} fields -
(
slice "What were the steps you have taken from the time when the symptom appeared to when the pain was addressed?
categorical [1…1]
fix exclusive
} ran;
) expand grid;
Output -
slice "What were the steps you have taken from the time when the symptom appeared to when the pain was addressed?
categorical [1…1]
fix exclusive
} ran;
There can be multiple instances of such pattern in a text file.
In Summary, I need to delete 2 parts here -
From the beginning of the line containing keyword 'loop' till keyword 'fields -'
Opening bracket (which will always be there below the line containing 'fields -') and closing bracket (which will always contain keyword 'grid;')
Upvotes: 1
Views: 110
Reputation: 7616
Your summary of what should happen, and your output (for given input) example do not match. I assume your summary is what you want.
You can use a regex with replace:
/[^\n\r]+loop[\s\S]*?fields[ \-\n\r\(]*([\s\S]*?)\) expand grid;[\n\r]+/g
Explanation:
[^\n\r]+loop
- scan over loop
line[\s\S]*?fields
- non-greedily scan over anything until fields
[ \-\n\r\(]*
- scan over additional characters we don't want([\s\S]*?)
- non-greedily scan over and capture anything...\) expand grid;
- ...until ) expand grid;
[\n\r]+
- scan over newlinesg
flag is for global, e.g. match multiple patterns like this$
or \1
, depending on your languageLet me use JavaScript with two repeating patterns to demo here on stackoverlow, you can easily translate that to your language:
const input = `Steps_taken "" loop
{
_1 "1st"
[
Sub cat = "_1,_2,_3,_4,_5,_6,_7"
],
_2 "2nd"
[
Sub cat = "_1,_2,_3,_4,_5,_6,_7,_98"
]
} fields -
(
slice "What were the steps of example 1?"
categorical [1…1]
fix exclusive
} ran;
) expand grid;
Steps_taken "" loop
{
_1 "1st"
[
Sub cat = "_1,_2,_3,_4,_5,_6,_7"
],
_2 "2nd"
[
Sub cat = "_1,_2,_3,_4,_5,_6,_7,_98"
]
} fields -
(
slice "What were the steps of example 2?"
categorical [2…2]
fix exclusive
} ran;
) expand grid;
`;
const regex = /[^\n\r]+loop[\s\S]*?fields[ \-\n\r\(]*([\s\S]*?)\) expand grid;[\n\r]+/g;
var result = input.replace(regex, '$1');
console.log('result:\n' + result);
Output:
result:
slice "What were the steps of example 1?"
categorical [1…1]
fix exclusive
} ran;
slice "What were the steps of example 2?"
categorical [2…2]
fix exclusive
} ran;
Upvotes: 1