Inserting Spaces Into a String That is a Defined Length

16 views
Skip to first unread message

Jeff Nyman

unread,
Apr 29, 2013, 5:52:00 PM4/29/13
to re...@googlegroups.com
I'm not sure how possible this is, but I'm curious to know if I can use regex to insert a space into a string if the string is 64 characters (or more) long. The string should be inserted at the half-way point. So if I have this string, for example:

thisisalongstringthatshouldbebrokenupbyaspacesothatlayoutissuesdonothappen

Then a space should be inserted into the string as such:

thisisalongstringthatshouldbebro    kenupbyaspacesothatlayoutissuesdonothappen

(I put extra spaces just to make it easier to see.)

I initially started with this:

^[a-zA-Z0-9]{64,}$

I wanted to check for 64 or more alphanumeric characters. That doesn't seem to work, so I tried this:

\s*[A-Za-z0-9]{64,}\s*$

I was hoping the above would at least tell me when I had 64 such characters so that I could then look up inserting characters. But I'm stuck even at this early stage.

Can anyone offer any pointers? I'm happy to try to work it out on my own, but I'm lacking the skills to pick up on the appropriate clues.

- Jeff

Prashant Patole

unread,
May 2, 2013, 1:49:29 AM5/2/13
to re...@googlegroups.com

find string 
(?:(\w{32})(\w{32,}))

replace string 
${1}     [spaces as much you want] ${2}


plz note 

\w = alphanumerics
\s = spaces
. = any char except new line
one personal suggestion :) :  in most cases starting with ^ and ending with $ is not required. so prefer it only when you got no tor option.

use expresso from 


prashant




--
--
Sub, Unsub, Read-on-the-web, tune your personal settings for this Regex forum:
http://groups.google.com/group/regex?hl=en
 
---
You received this message because you are subscribed to the Google Groups "Regex" group.
To unsubscribe from this group and stop receiving emails from it, send an email to regex+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Staffan Nöteberg

unread,
May 2, 2013, 3:28:52 AM5/2/13
to re...@googlegroups.com
Hi Jeff,

If you want to divide any string in half, regardless of its length,
then I don't think it's possible with regular expressions (in the
mathematical sense). That is, if the string happens to be 84 characters
long, then you want to insert a space after the 42nd character etc. It's
probably possible to solve with the recursive add-on that's present in
Ruby Regex and Perl Regex, but not with just ordinary regular
expressions.

If, on the other hand, the length is hardcoded to e.g. 64, it's
trivial. In Ruby, it is:

result =
'thisisalongstringthatshouldbebrokenupbyaspacesothatlayoutissuesd'.sub(/(.{32})(.*)/,
'\1 \2')


Best regards // Staffan
---
http://www.staffannoteberg.com/regexbook/


02.05.2013 07:49 skrev Prashant Patole:
> find string 
> (?:(w{32})(w{32,}))
>
> replace string 
> ${1}     [spaces as much you want] ${2}
>
> plz note 
>
> w = alphanumerics
> s = spaces
> . = any char except new line
> one personal suggestion :) :  in most cases starting with ^ and
> ending with $ is not required. so prefer it only when you got no tor
> option.
>
> use expresso from 
> http://www.ultrapico.com/ [4]
>
> prashant
> www.supersimplesoft.com [5]
>
> On Tue, Apr 30, 2013 at 3:22 AM, Jeff Nyman <jeff...@gmail.com [6]>
>> http://groups.google.com/group/regex?hl=en [1]
>>  
>> ---
>> You received this message because you are subscribed to the Google
>> Groups "Regex" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to regex+un...@googlegroups.com [2].
>> For more options, visit https://groups.google.com/groups/opt_out
>> [3].
>>  
>>  
>
> --
> --
> Sub, Unsub, Read-on-the-web, tune your personal settings for this
> Regex forum:
> http://groups.google.com/group/regex?hl=en [7]
>
> ---
> You received this message because you are subscribed to the Google
> Groups "Regex" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to regex+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out
> [8].
>
>
>
> Links:
> ------
> [1] http://groups.google.com/group/regex?hl=en
> [2] mailto:regex%2Bunsu...@googlegroups.com
> [3] https://groups.google.com/groups/opt_out
> [4] http://www.ultrapico.com/
> [5] http://www.supersimplesoft.com
> [6] mailto:jeff...@gmail.com
> [7] http://groups.google.com/group/regex?hl=en
> [8] https://groups.google.com/groups/opt_out

Staffan Nöteberg

unread,
May 2, 2013, 3:38:59 AM5/2/13
to re...@googlegroups.com
And one more thing... :-)

If you want the substitution to only take place, when there's at least
64 characters, it's easily accomplished with positive lookahead, like
this:

ruby>
'123456789.123456789.123456789.123456789.123456789.123456789.12345'.sub(/(?=.{64,})(.{32})(.*)/,
'\1 \2')
#=> "123456789.123456789.123456789.12
3456789.123456789.123456789.12345"
ruby>
'123456789.123456789.123456789.123456789.123456789.123456789.1234'.sub(/(?=.{64,})(.{32})(.*)/,
'\1 \2')
#=> "123456789.123456789.123456789.12
3456789.123456789.123456789.1234"
ruby>
'123456789.123456789.123456789.123456789.123456789.123456789.123'.sub(/(?=.{64,})(.{32})(.*)/,
'\1 \2')
#=>
"123456789.123456789.123456789.123456789.123456789.123456789.123"

Note that no space is inserted in the third case, when there's only 63
characters in the input.


Best regards // Staffan
---
http://www.staffannoteberg.com/regexbook/


> --

iiz

unread,
May 3, 2013, 3:17:42 AM5/3/13
to re...@googlegroups.com
Jeff,
 
I would not do this using regular expression, but with a combination of length, round and substring, not language specific, but pseudo code it would be something like
substring(string,0,round(length(string/2),0))||'    '||substring(string,round(length(string/2),0),)
which should cut your string exactly down the middle.
 
But if it was just an experiment, to see whether or not regex could do it, the $ sign is where you go wrong, since it anchers your string to the end where you wanted the first characters. ^\w{64} or ^.{64} will match your first 64 chararacters. You can then backreference them, or use match operant to put the string in an array, or use regexp_substring type method to operate on the matches.
 
Regards,
 
Theo.
 
 

Op maandag 29 april 2013 23:52:00 UTC+2 schreef Jeff Nyman het volgende:
Reply all
Reply to author
Forward
0 new messages