Copy link to clipboard
Copied
I need to insert space after each eight character in a string.
Eg:
<cfset mytoken ="ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCD">
<cfoutput>
<cfset replaceChar = ' '>
<cfset mytoken =Insert(replaceChar, mytoken, 8)>
<cfset mytoken =Insert(replaceChar, mytoken, 17)>
<cfset mytoken =Insert(replaceChar, mytoken, 26)>
<cfset mytoken =Insert(replaceChar, mytoken, 35)>
<cfset mytoken =Insert(replaceChar, mytoken, 44)>
<cfset mytoken =Insert(replaceChar, mytoken, 53)>
<cfset mytoken =Insert(replaceChar, mytoken, 62)>
mytoken:#mytoken#<br />
The above gives me result as
mytoken:ABCDEFGH IJKLMNOP QRSTUVWX YZABCDEF GHIJKLMN OPQRSTUV WXYZABCD
Can I achieve the same result using regular expresssion and reduce the code?
Not quite, but nearly. This:
reReplaceNoCase(mytoken, "([a-z][0-9]{1,8})", "\1 ", 'all')
Means "a letter, followed by one to eight numbers". You need:
reReplaceNoCase(mytoken, "([a-z|0-9]{1,8})", "\1 ", 'all')
Which means "one to eight letters or numbers". As Adam pointed out earlier, you can simplify it further:
reReplaceNoCase(mytoken, "([a-z|0-9]{8})", "\1 ", 'all')
Copy link to clipboard
Copied
Hell yeah you can!
Give this a bash:
reReplaceNoCase("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "([A-Z]{1,8})", "\1 ", 'all')
Copy link to clipboard
Copied
You'd just want {8} rather than {1,8} in there, wouldn't ya?
--
Adam
Copy link to clipboard
Copied
I did think that, but didn't know whether (although it worked) doing so would be technically valid for the remaining two characters at the end.
Thinking about it more in this case you don't want a space at the end anyway so yes, the {8} would do the job. I think I was considering the case that you wanted the last two as a group, who knows. Maybe I was drunk at the time.
As Adam points out - you can trim it down even more. A little more svelte than your original code I'd say
Copy link to clipboard
Copied
Thanks both of you.
If the string contains alphanumeric values,
<cfset mytoken ="ABC33FG5666JKLMNOPQRSTUVWXYZABCDEFG666KLMNOPQRSTUVW555ABCD">
then
I tried to change it as
reReplaceNoCase(mytoken, "([a-z][0-9]{1,8})", "\1 ", 'all')
But it doesn't yield the correct result.
The first set must be 'ABC33FG5'. But the above regular expression gave it as 'ABC33 FG5'.
Copy link to clipboard
Copied
Not quite, but nearly. This:
reReplaceNoCase(mytoken, "([a-z][0-9]{1,8})", "\1 ", 'all')
Means "a letter, followed by one to eight numbers". You need:
reReplaceNoCase(mytoken, "([a-z|0-9]{1,8})", "\1 ", 'all')
Which means "one to eight letters or numbers". As Adam pointed out earlier, you can simplify it further:
reReplaceNoCase(mytoken, "([a-z|0-9]{8})", "\1 ", 'all')
Copy link to clipboard
Copied
You don't need the | (OR) in the []. Everything inside square brackets is implicitly ORed by nature of how the character set construct works, eg:
[abc] means "an A, a B or a C". Similarly [a-z0-9] implicitly means "an A, a B... a Z, a 0... or a 9".
--
Adam
Copy link to clipboard
Copied
Good spot, my bad. This is why I should test things before posting on forums
Copy link to clipboard
Copied
Thanks again both of you for the support.
Copy link to clipboard
Copied
Adam Cameron. wrote:
You don't need the | (OR) in the []. Everything inside square brackets is implicitly ORed by nature of how the character set construct works, eg:
[abc] means "an A, a B or a C". Similarly [a-z0-9] implicitly means "an A, a B... a Z, a 0... or a 9".
The point you make is correct. However, I do believe your example should, in general, be case sensitive. That is, [abc] means one of a, b or c. Similarly, [a-z0-9] means one of a, b, c,..., z, or of 0,1,2,...,9.
Copy link to clipboard
Copied
Owain, Can u just tell me what does "\1" represents in regular expression?
Copy link to clipboard
Copied
It's called a backreference, and it means "insert the first group I made in my regex". You make groups simply by surrounding them by parentheses.
In this example, you use this:
([a-z][0-9]{1,8})
Which is the first (and in this case only) group in your regex. Therefore when you replace with this "\1 ", that means "replace it with the block of text you found followed by a space".
Hth.
Copy link to clipboard
Copied
Full documentation for CF's regex implementation can be found in the docs:
http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html
--
Adam