Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Insert space in a string

Guest
Jun 03, 2011 Jun 03, 2011

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?

1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jun 09, 2011 Jun 09, 2011

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')

Translate
Guide ,
Jun 03, 2011 Jun 03, 2011

Hell yeah you can!

Give this a bash:

reReplaceNoCase("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "([A-Z]{1,8})", "\1 ", 'all')

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 03, 2011 Jun 03, 2011

You'd just want {8} rather than {1,8} in there, wouldn't ya?

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 03, 2011 Jun 03, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 09, 2011 Jun 09, 2011

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'.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 09, 2011 Jun 09, 2011

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')

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 09, 2011 Jun 09, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 09, 2011 Jun 09, 2011

Good spot, my bad. This is why I should test things before posting on forums

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 09, 2011 Jun 09, 2011

Thanks again both of you for the support.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2011 Jun 13, 2011
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 09, 2011 Jun 09, 2011

Owain, Can u just tell me what does "\1" represents in regular expression?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 12, 2011 Jun 12, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 12, 2011 Jun 12, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources