Removing break and CrLf from text file

bogorman
bogorman used Ask the Experts™
on
I have generated, using vb.net, a text file containing content from a set of webpages.
As you will see from the attached file, most lines terminate with a <br> tag followed by a CrLf character, then a series of spaces followed by another <br>. Where this occurs I want to delete the first <br> and the CrLf character.   The number of spaces between the CrLf and the following <br> may vary.

I have tried using:

Body = Replace(Body, "<br>" + vbCrLf, "")

but it doesn't seem to work.   Also, if this coding worked, it does not check that the following spaces and <br> exist.
Screen-Shot-2012-04-14-at-14.18..png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Try this:

Body = System.Text.RegularExpressions.Regex.Replace(Body, "<br>[\r\n]+(?= *<br>)", String.Empty)

Open in new window

Author

Commented:
Thanks so much kaufmed. Works perfectly.

Can you answer a similar related question?

At present the paras are separated with break tags and crlfs. How can I replace these with para tags. Problem is that

the FIRST <br> tag followed by a CrLf character and the series of spaces followed by another <br> has to be replaced by <p>

and subsequent ones with </p><p>

and the last ones with </p>

Brian
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I think a few replacements would be in order for that:

Body = System.Text.RegularExpressions.Regex.Replace(Body, "^(.*?)<br>", "$1<p>")
Body = System.Text.RegularExpressions.Regex.Replace(Body, "<br>(.*?)$", "</p>$1")
Body = String.Replace(Body, "<br>", "</p>" & vbNewLine & "<p>")

Open in new window

Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
thanks for rapid response.
I get an Overload Resolution error on:

String.Replace(Body, "<br>", "</p>" & vbNewLine & "<p>")

Should it be Body.Replace("<br>", "</p>" & vbNewLine & "<p>")
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Yes. My fault  : \

Author

Commented:
It does not seem to replace the present spacing with para tags.  Part of one of the files:


On Bearing And Forebearing In Married Life| <p>
        <strong><br>
        Edward Holloway</strong> FAITH Magazine November-December 2002
      <br>
      <strong><br>
      Loving as you love yourself      </strong><br><br>So also ought men to love their wives as their own bodies.&nbsp;He that loves his wife loves himself.&nbsp;For no man ever hated his own flesh, but nourishes and cherishes it, as also does Christ the Church&rdquo;.&nbsp;The unity of mind and heart which St Paul here preaches (Eph. 5:28) to Christian husbands and wives goes beyond the mutual giving and receiving for life of the rights of marriage.&nbsp;It goes beyond the indissolubility of the marriage bond even, and sweeps out to embrace an ideal of mercy and mutual forbearance with each other&rsquo;s human shortcomings which married people do not always bring home to themselves as sincerely as they should.
      <br><br>If the love of husband and wife is the love of &ldquo;two in one flesh&rdquo; then this intimate love must show itself in the frailties of human life as kindliness and forbearance with each other in gentleness of spirit, for this is the graciousness of mercy which each one of us naturally concedes to himself, to his own flesh.&nbsp;When we stop to apply this teaching, the consequences are deeper and more practical than they appear at first glance.&nbsp;In our own defects and acts of meanness, none of us truly hates himself, however much he may be ashamed of himself.&nbsp;We are always willing to accept our own act of contrition with largeness of heart, and to take our purpose of amendment generously at its face value without cynicism.&nbsp;If there is one person who deserves another chance, and is going to get it when we are the criminal, it is our precious self, and, so long as this goodwill towards ourselves is joined with humility and sincerity, it is as it should be, and is the way God treats us with tireless patience in the sacrament of penance.&nbsp;The test for married people, and a test they can apply immediately, is whether they habitually behave in this way to each other in their life together, because if they do not, then they do not love each other with that gentleness of mercy which is part of Christian love, and part of the spiritual meaning of &ldquo;two in one flesh&rdquo;.&nbsp;
      <br><br>&lsquo;Bear&rsquo; and Forebear&rsquo;
      <br class="style1"><br>am told that in a less sophisticated age, and one more fond of art with a moral than the present, young couples would sometimes be presented with a pair of bears.&nbsp;These discreet animals stood at either end of the living-room mantelpiece wearing a tolerant smile, their arms over a plaque which bore their names, which were, naturally enough, &ldquo;Bear&rdquo; and &ldquo;Forebear&rdquo; respectively.&nbsp;When a very cross young wife was not on speaking terms with her husband, and was turning her eyes everywhere except across the table at his, they would eventually alight on that exasperating animal who was exhorting her to &ldquo;bear&rdquo; while her resentful husband would find himself glaring at the conciliatory &ldquo;forebear&rdquo;.&nbsp;If those animals survived the hazards of their mantelpiece mission, they must mostly have won the day, for the moment a man or woman begins to allow even the ghost of an inward smile at themselves, anger and sulky pride thins like a morning mist, and the devil and the powers of darkness are already in full retreat.&nbsp;In an age with a passion for scattering soulful dogs and winsome kittens all over the house, we might profitably use a few more bears.
      <br><br>
      <strong>Love needs constant renewal      </strong><br><br>It is the small frictions of married life which are the usual beginnings of serious trouble; it is rarely that some big cause of unhappiness comes suddenly out of the blue.&nbsp;It is the continual mishandling of incidents in themselves trivial that begets disillusionment, and then dislike.&nbsp;Men and women flare up at each other, behave with arrogance and sullenness and show that because they are bound for life, they think they can take each other for granted.&nbsp;This is a bad mistake, and an affront to a love born of the Faith, for no human being can ever be taken for granted, and mutual love is a gift which must be ever replenished from its sources, and those sources are in the goodness and nobility of the soul.
      <br><br>A loss of fervour in prayer, and neglect of God, is often the cause of the cooling of love in married life.&nbsp;Men and women begin unconsciously to seek their complete fulfilment in each other, and in the material interests of their home.&nbsp;They begin to forget that nobody, married or unmarried, can find his perfect satisfaction in another human being.&nbsp;To satisfy the heart completely is God&rsquo;s privilege only, and that sense of peace and purpose in life comes only with self-sacrifice and prayer.&nbsp;When men and women begin to make false gods of each other, they begin at once to demand a level of perfection which it is not just or fair to expect continuously of imperfect human beings, and as they become more querulous and selfish, their demands become unkind and unreasonable in any event, and they weary and depress their partner with their nervy irritability and unpredictable changes of mood.&nbsp;This is a sickness of soul, and a symptom of spiritual failure, and its understanding does not need all the nonsense masquerading as psychology poured out concerning it in the public press.&nbsp;
      <br>
      <strong><br>
      Without God, love slowly dies      </strong><br><br>It brings us back to the principle that when we are too lazy and selfish to love God, we lose everything else as well, and when we seek Him generously, the well-springs of the heart rise too, and with the love of God, we find love, laughter, and gentleness for our neighbour also.&nbsp;This defect, the idolising of a human being in the place due to God, is very common to &ldquo;in-laws&rdquo; too.&nbsp;Marriages are made unhappy, or even undermined by the possessive jealousy of small-minded parents, and this is a fault from which grave sin is never far absent.&nbsp;It is written that &ldquo;He&nbsp;who made man from the beginning, made them male and female, and for this cause shall a man leave father and mother, and cleave to his wife, and they shall be two in one flesh&rdquo;.&nbsp;The first loyalty of husband or wife is to each other, and possessive parents must not put asunder what God has joined together.
      <br><br>The rearing of a family of their own is the great natural support of love between husband and wife, for this it is which gives marriage its deepest meaning and purpose, and in these days of unruly children and increasing juvenile crime, it is as well to stress that the upbringing of children requires discipline and moral leadership as well as affection.&nbsp;I think it is unfair to suggest that the increased social and welfare services of the modern state are responsible for this lack of character in the modern child by usurping the duties of the parents.&nbsp;It seems to me that the whole cause is in the homes and the personalities of modern parents.&nbsp;An increasing number of families in Britain are being emptied of religious convictions, moral purpose, or inspiring hope in life.&nbsp;This modern home is the legacy of two generations of contempt of religion and neglect of God.&nbsp;When fifty years ago, people ceased to go to Church, they did not at once decline in moral vigour, for they retained a hard core of belief&nbsp;in God, the commandments, life after death, and the judgement of God; this they lived in their life and thoughts and passed on to their children with conviction.&nbsp;
      <br><br>
      <strong>Faith and prayer at the heart of life      </strong><br><br>To-day the decline of Christian doctrine and morals has gone so far that an increasing number of parents are living empty, dissipated, and purposeless lives, and have neither leadership nor a constructive love to give their children.&nbsp;Yet a child naturally seeks and expects this loving leadership, and when it does not obtain it will show its frustration in that petulant contempt for its parents, and other authority, which we see all round us to-day.&nbsp;Where the social betterment of life goes together with a good sound catholic home and thank God, we can see many of them in any parish, the child of to-day is better in body and in soul than any generation which has gone before it. Only God, working through the minds and hearts of good parents, can give sturdy moral goodness to a child, and this vocation of grace and love the state can neither supply nor supplant.&nbsp;Society can build upon this foundation, but if it crumbles, as it has begun to crumble, then whatever their good intentions, statesmen will be pouring ideals and money into the sand.
      <br><br>When one sees with sadness such a number of married people who deny their vocation and refuse to have a family, one wonders whether they have ever thought of the grim retribution of emptiness and loneliness of life which awaits them in a barren old age.&nbsp;There is no age in life more revealing, or more terrible in its contrasts than the late years of a man&rsquo;s life, for in those years the true personality, and the moral worth of individuals stands out with a stark clarity that no other period of life allows.&nbsp;Some men and women have matured with the years in wisdom, age, and grace, before God and men; while the body has faded, they have an increased measure of sweetness of disposition, mellowness, wisdom, and balance of judgement.&nbsp;Such men in the years of their physical weakness are fit in mind or in soul for the highest offices of Church or State, and are found holding them with distinction.&nbsp;Others, on the contrary, are more selfish, whining and disagreeable in their declining years, the onset of age seems to empty them out in body and soul alike.&nbsp;
      <br><br>
      <strong>What you sow, you will reap      </strong><br><br>The reason for this dramatic divergence of personalities in later years is not far to seek.&nbsp;When the zest, buoyancy and physical joy of youth evaporates, men and women are left with what they really are, with those gifts of personality which are not material, and of the body, but spiritual, and of the immortal soul.&nbsp;They are found with their hands full or empty, and there is none of the glitter of physical charm to camouflage the dross underneath.&nbsp;Men, who have matured in the virtues of the soul bear these gifts with them in dignity and power to the end of their days, while others, when the volatile spirit of the flesh has evaporated, are left with the dreary husk of worldliness and emptiness which is their real and unlovable self.&nbsp;In this contrast men and women begin to show forth even in this life the beginning of the judgement of God which St Paul preaches: &ldquo;Be not deceived, God is not mocked.&nbsp;For what things a man shall sow, those also he shall reap.&nbsp;For he that sows in his flesh, of the flesh also shall reap corruption, but he that sows in the spirit, of the spirit shall reap life everlasting&rdquo; (Gal 6.8).
      <br>
      <strong><br>
      The best wine saved to the last      </strong><br><br>It is here that the hardworking, often hard-pressed, husband and wife will find a rich reward that will be a pledge on earth of the blessing and reward of Christ which will crown the last day of their lives.&nbsp;They who have ripened through the years in understanding and selfless love will never forfeit the love and respect of their children, who even in adult years will seek them as their most valued counsellors.&nbsp;These parents will see their children&rsquo;s children rise up around them with joy, and love their greeting.&nbsp;They will know the best gift of life, the gift of love returned, when good and grateful children turn to them and call them blessed for all the years of love and labour gladly given, for there is a reverence in the simple words &ldquo;mother&rdquo; and &ldquo;dad&rdquo; in the later years of a parent&rsquo;s life which is almost sacred, and derives from the sanctity within Christian marriage and Christian parenthood.&nbsp;
      <br><br>In this serene happiness of their age good parents will also find their own love for each other more deep, because more spiritual and refined, than in the early years of life.&nbsp;As at the marriage feast of Cana, Christ turned water into wine, so over the years of life His grace mellows the water of romantic but immature love to the wine of a deeper and more spiritually perfect affection.&nbsp;And this He does that in every age those who do not know from whence the wine of Christian love has come, may exclaim with the steward in the gospel that &ldquo;every man at first sets forth good wine, and when men have well drunk, then that which is worse, but Thou has kept the good wine until now&rdquo; (John, 2.10).
      <br><br>&quot;As the sun when it rises to the world<br>
        in the high places of God, <br>
        so is the beauty of a good wife <br>
        for the adornment of her house. <br>
        As the lamp shining upon the holy candlestick, <br>
        so is the beauty of the face in a ripe age.&rdquo;
      <br><br>
      <em>(Ecclesiasticus 26: 21-22) </em><br>
      <br><br>

Author

Commented:
This may be more helpful - shows invisibles.
Screen-Shot-2012-04-15-at-16.04..png
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
I tested with this:

Module Module1

  Sub Main()
    Dim Body As String = System.IO.File.ReadAllText("input.txt")

    Body = System.Text.RegularExpressions.Regex.Replace(Body, "(?s)^(.*?)<br>", "$1<p>")
    Body = System.Text.RegularExpressions.Regex.Replace(Body, "<br>((?:[^<]|<(?!br>))*)$", "</p>$1")
    Body = Body.Replace("<br>", "</p>" & vbNewLine & "<p>")

    System.IO.File.WriteAllText("output.txt", Body)
  End Sub

End Module

Open in new window


...and it generated the following output:

On Bearing And Forebearing In Married Life| <p>
        <strong><p>
        Edward Holloway</strong> FAITH Magazine November-December 2002
      </p>
<p>
      <strong></p>
<p>
      Loving as you love yourself      </strong></p>
<p></p>
<p>So also ought men to love their wives as their own bodies.&nbsp;He that loves his wife loves himself.&nbsp;For no man ever hated his own flesh, but nourishes and cherishes it, as also does Christ the Church&rdquo;.&nbsp;The unity of mind and heart which St Paul here preaches (Eph. 5:28) to Christian husbands and wives goes beyond the mutual giving and receiving for life of the rights of marriage.&nbsp;It goes beyond the indissolubility of the marriage bond even, and sweeps out to embrace an ideal of mercy and mutual forbearance with each other&rsquo;s human shortcomings which married people do not always bring home to themselves as sincerely as they should. 
      </p>
<p></p>
<p>If the love of husband and wife is the love of &ldquo;two in one flesh&rdquo; then this intimate love must show itself in the frailties of human life as kindliness and forbearance with each other in gentleness of spirit, for this is the graciousness of mercy which each one of us naturally concedes to himself, to his own flesh.&nbsp;When we stop to apply this teaching, the consequences are deeper and more practical than they appear at first glance.&nbsp;In our own defects and acts of meanness, none of us truly hates himself, however much he may be ashamed of himself.&nbsp;We are always willing to accept our own act of contrition with largeness of heart, and to take our purpose of amendment generously at its face value without cynicism.&nbsp;If there is one person who deserves another chance, and is going to get it when we are the criminal, it is our precious self, and, so long as this goodwill towards ourselves is joined with humility and sincerity, it is as it should be, and is the way God treats us with tireless patience in the sacrament of penance.&nbsp;The test for married people, and a test they can apply immediately, is whether they habitually behave in this way to each other in their life together, because if they do not, then they do not love each other with that gentleness of mercy which is part of Christian love, and part of the spiritual meaning of &ldquo;two in one flesh&rdquo;.&nbsp;
      </p>
<p></p>
<p>&lsquo;Bear&rsquo; and Forebear&rsquo;
      <br class="style1"></p>
<p>am told that in a less sophisticated age, and one more fond of art with a moral than the present, young couples would sometimes be presented with a pair of bears.&nbsp;These discreet animals stood at either end of the living-room mantelpiece wearing a tolerant smile, their arms over a plaque which bore their names, which were, naturally enough, &ldquo;Bear&rdquo; and &ldquo;Forebear&rdquo; respectively.&nbsp;When a very cross young wife was not on speaking terms with her husband, and was turning her eyes everywhere except across the table at his, they would eventually alight on that exasperating animal who was exhorting her to &ldquo;bear&rdquo; while her resentful husband would find himself glaring at the conciliatory &ldquo;forebear&rdquo;.&nbsp;If those animals survived the hazards of their mantelpiece mission, they must mostly have won the day, for the moment a man or woman begins to allow even the ghost of an inward smile at themselves, anger and sulky pride thins like a morning mist, and the devil and the powers of darkness are already in full retreat.&nbsp;In an age with a passion for scattering soulful dogs and winsome kittens all over the house, we might profitably use a few more bears.
      </p>
<p></p>
<p>
      <strong>Love needs constant renewal      </strong></p>
<p></p>
<p>It is the small frictions of married life which are the usual beginnings of serious trouble; it is rarely that some big cause of unhappiness comes suddenly out of the blue.&nbsp;It is the continual mishandling of incidents in themselves trivial that begets disillusionment, and then dislike.&nbsp;Men and women flare up at each other, behave with arrogance and sullenness and show that because they are bound for life, they think they can take each other for granted.&nbsp;This is a bad mistake, and an affront to a love born of the Faith, for no human being can ever be taken for granted, and mutual love is a gift which must be ever replenished from its sources, and those sources are in the goodness and nobility of the soul. 
      </p>
<p></p>
<p>A loss of fervour in prayer, and neglect of God, is often the cause of the cooling of love in married life.&nbsp;Men and women begin unconsciously to seek their complete fulfilment in each other, and in the material interests of their home.&nbsp;They begin to forget that nobody, married or unmarried, can find his perfect satisfaction in another human being.&nbsp;To satisfy the heart completely is God&rsquo;s privilege only, and that sense of peace and purpose in life comes only with self-sacrifice and prayer.&nbsp;When men and women begin to make false gods of each other, they begin at once to demand a level of perfection which it is not just or fair to expect continuously of imperfect human beings, and as they become more querulous and selfish, their demands become unkind and unreasonable in any event, and they weary and depress their partner with their nervy irritability and unpredictable changes of mood.&nbsp;This is a sickness of soul, and a symptom of spiritual failure, and its understanding does not need all the nonsense masquerading as psychology poured out concerning it in the public press.&nbsp;
      </p>
<p>
      <strong></p>
<p>
      Without God, love slowly dies      </strong></p>
<p></p>
<p>It brings us back to the principle that when we are too lazy and selfish to love God, we lose everything else as well, and when we seek Him generously, the well-springs of the heart rise too, and with the love of God, we find love, laughter, and gentleness for our neighbour also.&nbsp;This defect, the idolising of a human being in the place due to God, is very common to &ldquo;in-laws&rdquo; too.&nbsp;Marriages are made unhappy, or even undermined by the possessive jealousy of small-minded parents, and this is a fault from which grave sin is never far absent.&nbsp;It is written that &ldquo;He&nbsp;who made man from the beginning, made them male and female, and for this cause shall a man leave father and mother, and cleave to his wife, and they shall be two in one flesh&rdquo;.&nbsp;The first loyalty of husband or wife is to each other, and possessive parents must not put asunder what God has joined together. 
      </p>
<p></p>
<p>The rearing of a family of their own is the great natural support of love between husband and wife, for this it is which gives marriage its deepest meaning and purpose, and in these days of unruly children and increasing juvenile crime, it is as well to stress that the upbringing of children requires discipline and moral leadership as well as affection.&nbsp;I think it is unfair to suggest that the increased social and welfare services of the modern state are responsible for this lack of character in the modern child by usurping the duties of the parents.&nbsp;It seems to me that the whole cause is in the homes and the personalities of modern parents.&nbsp;An increasing number of families in Britain are being emptied of religious convictions, moral purpose, or inspiring hope in life.&nbsp;This modern home is the legacy of two generations of contempt of religion and neglect of God.&nbsp;When fifty years ago, people ceased to go to Church, they did not at once decline in moral vigour, for they retained a hard core of belief&nbsp;in God, the commandments, life after death, and the judgement of God; this they lived in their life and thoughts and passed on to their children with conviction.&nbsp;
      </p>
<p></p>
<p>
      <strong>Faith and prayer at the heart of life      </strong></p>
<p></p>
<p>To-day the decline of Christian doctrine and morals has gone so far that an increasing number of parents are living empty, dissipated, and purposeless lives, and have neither leadership nor a constructive love to give their children.&nbsp;Yet a child naturally seeks and expects this loving leadership, and when it does not obtain it will show its frustration in that petulant contempt for its parents, and other authority, which we see all round us to-day.&nbsp;Where the social betterment of life goes together with a good sound catholic home and thank God, we can see many of them in any parish, the child of to-day is better in body and in soul than any generation which has gone before it. Only God, working through the minds and hearts of good parents, can give sturdy moral goodness to a child, and this vocation of grace and love the state can neither supply nor supplant.&nbsp;Society can build upon this foundation, but if it crumbles, as it has begun to crumble, then whatever their good intentions, statesmen will be pouring ideals and money into the sand. 
      </p>
<p></p>
<p>When one sees with sadness such a number of married people who deny their vocation and refuse to have a family, one wonders whether they have ever thought of the grim retribution of emptiness and loneliness of life which awaits them in a barren old age.&nbsp;There is no age in life more revealing, or more terrible in its contrasts than the late years of a man&rsquo;s life, for in those years the true personality, and the moral worth of individuals stands out with a stark clarity that no other period of life allows.&nbsp;Some men and women have matured with the years in wisdom, age, and grace, before God and men; while the body has faded, they have an increased measure of sweetness of disposition, mellowness, wisdom, and balance of judgement.&nbsp;Such men in the years of their physical weakness are fit in mind or in soul for the highest offices of Church or State, and are found holding them with distinction.&nbsp;Others, on the contrary, are more selfish, whining and disagreeable in their declining years, the onset of age seems to empty them out in body and soul alike.&nbsp;
      </p>
<p></p>
<p>
      <strong>What you sow, you will reap      </strong></p>
<p></p>
<p>The reason for this dramatic divergence of personalities in later years is not far to seek.&nbsp;When the zest, buoyancy and physical joy of youth evaporates, men and women are left with what they really are, with those gifts of personality which are not material, and of the body, but spiritual, and of the immortal soul.&nbsp;They are found with their hands full or empty, and there is none of the glitter of physical charm to camouflage the dross underneath.&nbsp;Men, who have matured in the virtues of the soul bear these gifts with them in dignity and power to the end of their days, while others, when the volatile spirit of the flesh has evaporated, are left with the dreary husk of worldliness and emptiness which is their real and unlovable self.&nbsp;In this contrast men and women begin to show forth even in this life the beginning of the judgement of God which St Paul preaches: &ldquo;Be not deceived, God is not mocked.&nbsp;For what things a man shall sow, those also he shall reap.&nbsp;For he that sows in his flesh, of the flesh also shall reap corruption, but he that sows in the spirit, of the spirit shall reap life everlasting&rdquo; (Gal 6.8). 
      </p>
<p>
      <strong></p>
<p>
      The best wine saved to the last      </strong></p>
<p></p>
<p>It is here that the hardworking, often hard-pressed, husband and wife will find a rich reward that will be a pledge on earth of the blessing and reward of Christ which will crown the last day of their lives.&nbsp;They who have ripened through the years in understanding and selfless love will never forfeit the love and respect of their children, who even in adult years will seek them as their most valued counsellors.&nbsp;These parents will see their children&rsquo;s children rise up around them with joy, and love their greeting.&nbsp;They will know the best gift of life, the gift of love returned, when good and grateful children turn to them and call them blessed for all the years of love and labour gladly given, for there is a reverence in the simple words &ldquo;mother&rdquo; and &ldquo;dad&rdquo; in the later years of a parent&rsquo;s life which is almost sacred, and derives from the sanctity within Christian marriage and Christian parenthood.&nbsp;
      </p>
<p></p>
<p>In this serene happiness of their age good parents will also find their own love for each other more deep, because more spiritual and refined, than in the early years of life.&nbsp;As at the marriage feast of Cana, Christ turned water into wine, so over the years of life His grace mellows the water of romantic but immature love to the wine of a deeper and more spiritually perfect affection.&nbsp;And this He does that in every age those who do not know from whence the wine of Christian love has come, may exclaim with the steward in the gospel that &ldquo;every man at first sets forth good wine, and when men have well drunk, then that which is worse, but Thou has kept the good wine until now&rdquo; (John, 2.10). 
      </p>
<p></p>
<p>&quot;As the sun when it rises to the world</p>
<p>
        in the high places of God, </p>
<p>
        so is the beauty of a good wife </p>
<p>
        for the adornment of her house. </p>
<p>
        As the lamp shining upon the holy candlestick, </p>
<p>
        so is the beauty of the face in a ripe age.&rdquo; 
      </p>
<p></p>
<p>
      <em>(Ecclesiasticus 26: 21-22) </em></p>
<p>
      </p>
<p></p>

Open in new window


Note:  I did make a small change to the 2nd regular expression.

Author

Commented:
It seems to work almost perfectly except it inserts <p> tags which seem to separate the <strong> tags and cause errors in the html.  For example:

On Bearing And Forebearing In Married Life| <p>
        <strong></p>
<p>
        Edward Holloway</strong> FAITH Magazine November-December 2002
      </p>
<p>
      <strong></p>
<p>
      Loving as you love yourself      </strong></p>

If you remove the <p> and </p> tags jusgt before "Edward Holloway", it removes one of the errors.

It may not be possible to cure this, kaufmed, but it would be nice if I could.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
It seems to work almost perfectly except it inserts <p> tags which seem to separate the <strong> tags and cause errors in the html.
That's because the <br> falls within the opening and closing <strong>. This is not standardized HTML. What you are talking about doing now is really getting into HTML parsing, which regex is not suitable for (alone). What you might consider doing is using something like the HTML Agility Pack to load the document and get access to a DOM. Then you could step through each node and make a decision as to whether or not to keep or discard it.

You may still get away with a simple replace as the <strong> tags appear to be the only tags with interior <br> tags, but I haven't done an extensive review of the text, so I cannot say for certain. Aside from that, I'm sure the above is only a sampling of what you are working with. There's no telling how the other documents are formatted (though one would hope that they all follow a similar structure). It really depends on how the documents were generated.

Author

Commented:
Thanks so much, kaufmed, for all the work you have done on this. It seems to be only the <strong> tags that are affected.
Think I may have to just put up with the large spacing on these old nodes. The strange thing is that, once I have got the content into drupal and I then just save the node the spacing is corrected, so bulk-saving of all nodes would do the trick. Will have to research how to do this.
Again, thanks. Will assign the points.
Brian

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial