Replacing newline characters with other text with regexp

Hi all,

I've searched around and did not find an answer...
I'm trying to replace all occurrences of newline characters (\n) with with an HTML BR sequence, I thought this would work:
 

Regexp hasNewLines = regexp = "([^\n]*)[\n](.*)$"
// I'm using XX instead of the HTML tag here
string htmlBR = "XX"
 
string fix (string original)
{
    string fixed = original
    while (hasNewLines fixed)
    {
        fixed = fixed[match 1] htmlBR fixed[match 2]
    }
    return fixed
}

 


This works if there is a single newline in the string, however, the string is truncated starting at the second occurrence of a newline.
So....

 

 

s = fix("AAA\nBBB\nCCC")


yields s = "AAAXXBBB"
NOT s = "AAAXXBBBXXCCC"

Can anybody assist with this?
Is there a better way to accomplish the same goal?
Thanks

 


SystemAdmin - Wed Aug 26 14:11:24 EDT 2009

Re: Replacing newline characters with other text with regexp
Ron_Lewis - Thu Aug 27 07:54:38 EDT 2009

Here is a simpler way for those who don't know RE; may take longer than RE depending on task.
 

//DemoReplace
/*Replace newline with <br>*/
Buffer b=create 
string s="line 1\nline 2\nline 3"
int j,l=length s
for(j=0;j<l;j++)
{ if(s[j]=='\n') b+="<br>"
    else  b+=s[j]""
}
print b ""

Re: Replacing newline characters with other text with regexp
dpechacek - Thu Aug 27 10:00:06 EDT 2009

Pretty much straight from the DXL Reference Manual.
 

Regexp newLine = regexp(".*");
string htmlBR = "<br>";
 
string fix (string original)
{
    string fixed = ""
    while(!null(original) && newLine original) {
        fixed = fixed original[match 0] htmlBR
        original = original[end 0 + 2:];
    }
    
    return fixed
}
 
print fix("AAA\nBBB\nCCC")

 


AAI Services, Textron
dpechacek@aai.textron.com
David.Pechacek@gmail.com

 

Re: Replacing newline characters with other text with regexp
dpechacek - Thu Aug 27 10:02:18 EDT 2009

dpechacek - Thu Aug 27 10:00:06 EDT 2009

Pretty much straight from the DXL Reference Manual.
 

Regexp newLine = regexp(".*");
string htmlBR = "<br>";
 
string fix (string original)
{
    string fixed = ""
    while(!null(original) && newLine original) {
        fixed = fixed original[match 0] htmlBR
        original = original[end 0 + 2:];
    }
    
    return fixed
}
 
print fix("AAA\nBBB\nCCC")

 


AAI Services, Textron
dpechacek@aai.textron.com
David.Pechacek@gmail.com

 

Yours wasn't working because comparing a string to a regular expression gets all the matches, not just the first two. So you were getting only match 0 and match 1 and throwing out the rest resulting in the loop only running once.

AAI Services, Textron
dpechacek@aai.textron.com
David.Pechacek@gmail.com

Re: Replacing newline characters with other text with regexp
Ron_Lewis - Thu Aug 27 11:48:49 EDT 2009

dpechacek - Thu Aug 27 10:00:06 EDT 2009

Pretty much straight from the DXL Reference Manual.
 

Regexp newLine = regexp(".*");
string htmlBR = "<br>";
 
string fix (string original)
{
    string fixed = ""
    while(!null(original) && newLine original) {
        fixed = fixed original[match 0] htmlBR
        original = original[end 0 + 2:];
    }
    
    return fixed
}
 
print fix("AAA\nBBB\nCCC")

 


AAI Services, Textron
dpechacek@aai.textron.com
David.Pechacek@gmail.com

 

Dave, I didn't offer the solution that you posted because technically it did not meet the reequirements. Your solution posts an extra
at end of line even thought end of line is not a newline character.

Re: Replacing newline characters with other text with regexp
Ron_Lewis - Thu Aug 27 11:54:42 EDT 2009

Ron_Lewis - Thu Aug 27 11:48:49 EDT 2009
Dave, I didn't offer the solution that you posted because technically it did not meet the reequirements. Your solution posts an extra
at end of line even thought end of line is not a newline character.

woops the forum messed up previous message

Dave,
I did not offer the solution that you posted because technically
it did not meet the requirements.
Your solution posts an extra <br> at end of line
even thought end of line is not a newline character.
 

Re: Replacing newline characters with other text with regexp
SystemAdmin - Thu Aug 27 11:58:06 EDT 2009

dpechacek - Thu Aug 27 10:00:06 EDT 2009

Pretty much straight from the DXL Reference Manual.
 

Regexp newLine = regexp(".*");
string htmlBR = "<br>";
 
string fix (string original)
{
    string fixed = ""
    while(!null(original) && newLine original) {
        fixed = fixed original[match 0] htmlBR
        original = original[end 0 + 2:];
    }
    
    return fixed
}
 
print fix("AAA\nBBB\nCCC")

 


AAI Services, Textron
dpechacek@aai.textron.com
David.Pechacek@gmail.com

 

Doh! Its on page 102 of the manual I have, somehow missed it.
Thanks

Re: Replacing newline characters with other text with regexp
llandale - Thu Aug 27 15:51:15 EDT 2009

Add this statement in your while loop before you update 'fixed'.

print "\t[" fixed[match 0] "]    [" fixed[match 1] "]    [" fixed[match 2] "]    [" fixed[match 3] "]\n"


Notice that match 2 is just BBB.

Read your regexp table in the DXL manual again. See that "." means any character except newline. So match 2 will include what's after the first NL up to the next NL, in this case BBB. I would have thought that by announcing 'end of string' with '$' that you would get no match. Anyway, to get any number of any characters at all, use

 

(.|\n)*    // that's "Any character except new line, or new line" any number of times


So your first line should look like this:

 

 

Regexp hasNewLines = regexp "([^\n]*)[\n]((.|\n)*)$"

 

 

  • Louie

 

Re: Replacing newline characters with other text with regexp
SystemAdmin - Thu Aug 27 17:24:18 EDT 2009

llandale - Thu Aug 27 15:51:15 EDT 2009

Add this statement in your while loop before you update 'fixed'.

print "\t[" fixed[match 0] "]    [" fixed[match 1] "]    [" fixed[match 2] "]    [" fixed[match 3] "]\n"


Notice that match 2 is just BBB.

Read your regexp table in the DXL manual again. See that "." means any character except newline. So match 2 will include what's after the first NL up to the next NL, in this case BBB. I would have thought that by announcing 'end of string' with '$' that you would get no match. Anyway, to get any number of any characters at all, use

 

(.|\n)*    // that's "Any character except new line, or new line" any number of times


So your first line should look like this:

 

 

Regexp hasNewLines = regexp "([^\n]*)[\n]((.|\n)*)$"

 

 

  • Louie

 

I was thinking Perl and looking for the "'.' includes newline" option. I will give the "(.|\n)*" a try, I was thinking "(http://.\n+)", when I get the basic functionality working. I rushed through this part of the program as it was a small part of what I was trying to accomplish, although it did break everything.
Thanks

(This is an excellent forum - quick and accurate response and only a subtle "RTFM"!)

Re: Replacing newline characters with other text with regexp
llandale - Fri Aug 28 09:23:39 EDT 2009

SystemAdmin - Thu Aug 27 17:24:18 EDT 2009
I was thinking Perl and looking for the "'.' includes newline" option. I will give the "(.|\n)*" a try, I was thinking "(http://.\n+)", when I get the basic functionality working. I rushed through this part of the program as it was a small part of what I was trying to accomplish, although it did break everything.
Thanks

(This is an excellent forum - quick and accurate response and only a subtle "RTFM"!)

... that's because TFM is a reference telling what the commands do, it doesn't teach and has few examples. Once you have RTFM, though, its actually useful since you know what to look for.

Re: Replacing newline characters with other text with regexp
SystemAdmin - Fri Aug 28 11:12:16 EDT 2009

llandale - Fri Aug 28 09:23:39 EDT 2009
... that's because TFM is a reference telling what the commands do, it doesn't teach and has few examples. Once you have RTFM, though, its actually useful since you know what to look for.

At the risk of going off-topic, I have googled quite a bit for a DXL tutorial, having found only the beginnings of tutorials, one author replied to my query "Where is page 2" with "I never got around to it".

I would much rather run though a tutorial and learn the language that way than make a fool of myself in these forums, I just have not been able to find one. OReilly does not have one, neither does Amazon, searches for DXL turn up a wide variety of results. I've learned quite a bit from the examples, however, some of them demonstrate only the syntax, and not all the nuances.

Is there a tutorial out there, or do I learn this "by the seat of my pants"?

I again thank everyone for their help, knowing that they are at least as busy as I am.

Re: Replacing newline characters with other text with regexp
Ron_Lewis - Fri Aug 28 12:29:00 EDT 2009

SystemAdmin - Fri Aug 28 11:12:16 EDT 2009
At the risk of going off-topic, I have googled quite a bit for a DXL tutorial, having found only the beginnings of tutorials, one author replied to my query "Where is page 2" with "I never got around to it".

I would much rather run though a tutorial and learn the language that way than make a fool of myself in these forums, I just have not been able to find one. OReilly does not have one, neither does Amazon, searches for DXL turn up a wide variety of results. I've learned quite a bit from the examples, however, some of them demonstrate only the syntax, and not all the nuances.

Is there a tutorial out there, or do I learn this "by the seat of my pants"?

I again thank everyone for their help, knowing that they are at least as busy as I am.

If you have deep pockets look here:
https://www-304.ibm.com/jct03001c/services/learning/ites.wss/us/en?pageType=course_description&courseCode=QN116

Re: Replacing newline characters with other text with regexp
llandale - Fri Aug 28 13:00:06 EDT 2009

SystemAdmin - Fri Aug 28 11:12:16 EDT 2009
At the risk of going off-topic, I have googled quite a bit for a DXL tutorial, having found only the beginnings of tutorials, one author replied to my query "Where is page 2" with "I never got around to it".

I would much rather run though a tutorial and learn the language that way than make a fool of myself in these forums, I just have not been able to find one. OReilly does not have one, neither does Amazon, searches for DXL turn up a wide variety of results. I've learned quite a bit from the examples, however, some of them demonstrate only the syntax, and not all the nuances.

Is there a tutorial out there, or do I learn this "by the seat of my pants"?

I again thank everyone for their help, knowing that they are at least as busy as I am.

There was an old v4 DXL manual that was incomplete ..err.. truncated, but I cannot find it.

Looking at examples is very useful. Make sure you understand each one, often going to the manual to figure out exactly what each statement is doing. Start by grasping all the snippettes you see on this forum, and advance to more complicated scripts. Modify some script to make them work a little differently. When you are reasonably comfortable understanding how the manual describes functions, then read the manual front to back. For example: ... bool f1(int Type, int f2(string)) ... means that perm f1 return boolean, and accepts two parameters, first it type int and second is a function that returns type int and requires a string parameter.

You probably won't get a bunch of nuances in a tutorial anyway, such as you do NOT use the "&" charcter for array function parameters; otherwise DXL goes into never-never land when you modify the array.
Don't do this: void SortArray(string &sArray[])
You won't find tutorials telling you which assignment statements assign the value and which ones define aliases. That's strange, since that's a pretty basic operation and should be in the manual.
... History h2 = h1 // Alias
... Buffer b2 = b1 // Alias
... Module m2 = m1 // Value
... etc

  • Louie

Re: Replacing newline characters with other text with regexp
mcnairk - Fri Aug 28 13:20:11 EDT 2009

SystemAdmin - Fri Aug 28 11:12:16 EDT 2009
At the risk of going off-topic, I have googled quite a bit for a DXL tutorial, having found only the beginnings of tutorials, one author replied to my query "Where is page 2" with "I never got around to it".

I would much rather run though a tutorial and learn the language that way than make a fool of myself in these forums, I just have not been able to find one. OReilly does not have one, neither does Amazon, searches for DXL turn up a wide variety of results. I've learned quite a bit from the examples, however, some of them demonstrate only the syntax, and not all the nuances.

Is there a tutorial out there, or do I learn this "by the seat of my pants"?

I again thank everyone for their help, knowing that they are at least as busy as I am.

Is this the tutorial you found: http://www.baselinesinc.com/?p=9

Ken.

Re: Replacing newline characters with other text with regexp
SystemAdmin - Fri Aug 28 13:30:05 EDT 2009

SystemAdmin - Fri Aug 28 11:12:16 EDT 2009
At the risk of going off-topic, I have googled quite a bit for a DXL tutorial, having found only the beginnings of tutorials, one author replied to my query "Where is page 2" with "I never got around to it".

I would much rather run though a tutorial and learn the language that way than make a fool of myself in these forums, I just have not been able to find one. OReilly does not have one, neither does Amazon, searches for DXL turn up a wide variety of results. I've learned quite a bit from the examples, however, some of them demonstrate only the syntax, and not all the nuances.

Is there a tutorial out there, or do I learn this "by the seat of my pants"?

I again thank everyone for their help, knowing that they are at least as busy as I am.

Ian Alexander has also some DXL tutorials on his pages at

http://easyweb.easynet.co.uk/~iany/consultancy/papers_welcome.htm#DOORSDXL

Re: Replacing newline characters with other text with regexp
kbmurphy - Tue Jun 15 11:32:03 EDT 2010

SystemAdmin - Fri Aug 28 13:30:05 EDT 2009
Ian Alexander has also some DXL tutorials on his pages at

http://easyweb.easynet.co.uk/~iany/consultancy/papers_welcome.htm#DOORSDXL

Ok--so the example given replaces a "\n" with a "
". Let's say I wanted to replace "\n\n" with a "\n". In other words, I want to remove an empty line. I found this incredibly hard to do with DXL Regex implementation as it can only do one line at a time.

I found a workaround for my issue--but wasn't satisfied that I couldn't figure it out in DXL. Any takers to this challenge?

Re: Replacing newline characters with other text with regexp
Mathias Mamsch - Tue Jun 15 17:25:32 EDT 2010

kbmurphy - Tue Jun 15 11:32:03 EDT 2010
Ok--so the example given replaces a "\n" with a "
". Let's say I wanted to replace "\n\n" with a "\n". In other words, I want to remove an empty line. I found this incredibly hard to do with DXL Regex implementation as it can only do one line at a time.

I found a workaround for my issue--but wasn't satisfied that I couldn't figure it out in DXL. Any takers to this challenge?

I throw the replace() implementation of the DXL standard library in the ring! It is reasonably fast for short strings (<5000 chars). Regards, Mathias

Note: Replace the raiseError function by a normal error function
 

/*! \return Returns sSource, where the searchstring \em sSearch is replaced by sReplace 
    \param sSource the source string in which \em sSearch shall be replaced
    \param sSearch the search string that shall be replaced
    \param sReplace the string by which \em sSearch shall be replaced
    \brief Replaces a part of the string by another string
*/  
string replace (string sSource, string sSearch, string sReplace) {
    int iLen = length sSource
    if (iLen == 0) return ""
    
    int iLenSearch = length(sSearch)
    
    if (iLenSearch == 0) { 
        raiseError ("Parameter error", "in strings.inc/replace: search string must not be empty")
        return "" 
    }
    
    // read the first char for latter comparison -> speed optimization
    char firstChar = sSearch[0]
    
    Buffer s = create() 
    int pos = 0, d1,d2;    
    int i
    
    while (pos < iLen) { 
        char ch = sSource[pos]; 
        bool found = true
        
        if (ch != firstChar) {pos ++; s+= ch; continue}
        for (i = 1; i < iLenSearch; i++) 
           if (sSource[pos+i] != sSearch[i]) { found = false; break }
        if (!found) {pos++; s+= ch; continue}
        s += sReplace
        pos += iLenSearch
    }
    
    string result = stringOf s
    delete s
    return result
}

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

 

Re: Replacing newline characters with other text with regexp
llandale - Tue Jun 15 20:19:54 EDT 2010

The buffer replace has got to be orders of magnitude faster than regular expressions, which will surely chew up string table space.

If you print your match 2 you'll see it does not include any C characters. That's because the period does not include any new lines (read that regexp table in the manual carefully). Combine that with the fact that the dollar sign means 'end of string or end of line', then any 3rd line doesn't get included.

I could not make this work. Like I said I'm real clumsy with Regular expressions, but looking back through my cob-webs of code I see these two interesting things:
[1] "()"
[2] "(\n*)"
Both are supposed to have the meaning "any number of any characters including EOL" in a regular expression; but frankly I don't remember what the deal is.

  • Louie

Re: Replacing newline characters with other text with regexp
shrunavi - Mon Aug 14 02:49:13 EDT 2017

llandale - Tue Jun 15 20:19:54 EDT 2010
The buffer replace has got to be orders of magnitude faster than regular expressions, which will surely chew up string table space.

If you print your match 2 you'll see it does not include any C characters. That's because the period does not include any new lines (read that regexp table in the manual carefully). Combine that with the fact that the dollar sign means 'end of string or end of line', then any 3rd line doesn't get included.

I could not make this work. Like I said I'm real clumsy with Regular expressions, but looking back through my cob-webs of code I see these two interesting things:
[1] "()"
[2] "(\n*)"
Both are supposed to have the meaning "any number of any characters including EOL" in a regular expression; but frankly I don't remember what the deal is.

  • Louie

Hello Team,

I have a string as below in blue and I want to split into three parts like

Input
The following data is needed for this voting logic:
ASCB Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Internal Data
None

1. I should check for the string should start with Input

2. I need first 3 lines into one string

Input
The following data is needed for this voting logic:
ASCB Data

3. From 4th line till the Internal Data I need separately like 

HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

4. From Internal Data to None into separate string

Internal Data
None

5, After internal data if anystring apart from None then I want that to be separate string

 

Please help me

Basically my task is to make bold for the text from 4th line until internal data and after internal data if any data then I should make bold for that also and the rest as normal text as shown below

Ex: 1

Input
The following data is needed for this voting logic:
ASCB Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Internal Data
None

Ex:2

Input
The following data is needed for this voting logic:
ASCB Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Internal Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Kindly help me how to do this in dxl

 

Regards,

Shrunavi

Re: Replacing newline characters with other text with regexp
Mathias Mamsch - Mon Aug 14 11:03:43 EDT 2017

shrunavi - Mon Aug 14 02:49:13 EDT 2017

Hello Team,

I have a string as below in blue and I want to split into three parts like

Input
The following data is needed for this voting logic:
ASCB Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Internal Data
None

1. I should check for the string should start with Input

2. I need first 3 lines into one string

Input
The following data is needed for this voting logic:
ASCB Data

3. From 4th line till the Internal Data I need separately like 

HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

4. From Internal Data to None into separate string

Internal Data
None

5, After internal data if anystring apart from None then I want that to be separate string

 

Please help me

Basically my task is to make bold for the text from 4th line until internal data and after internal data if any data then I should make bold for that also and the rest as normal text as shown below

Ex: 1

Input
The following data is needed for this voting logic:
ASCB Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Internal Data
None

Ex:2

Input
The following data is needed for this voting logic:
ASCB Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Internal Data
HCM1_hcmone1000msec_good
sdfds_sdsdfsdfsd_sdfdsf
sdfsdf_sdfsd_sdfsdfsd

Kindly help me how to do this in dxl

 

Regards,

Shrunavi

Search the forum for regexp examples and do not pull such old posts! Instead open a new post. Use a minimal example. Try to come up with some code on your own and ask specific questions instead of "Please code this for me" questions. Regards, Mathias