Ask Your Question
2

Question about regsubst

asked 2015-08-06 19:30:29 -0500

R gravatar image

I have a string called $str="mybook", I want to split the string $str into two variables:

Output should like this the first variable should be $str1="my" $str2="book". Equivalent to UNIX command cut-c. This link talks https://docs.puppetlabs.com/reference... talk only if there is a delimiter. My question is what if there is no delimiter?

Thanks in advance

edit retag flag offensive close merge delete

Comments

How are you deciding where to split the string? Do you always want to split the 1st 2 characters off, or is there some other rule?

GregLarkin gravatar imageGregLarkin ( 2015-08-06 20:24:28 -0500 )edit

Thanks Greg. I always want to split the first 3 characters to one variable ans remaining characters to another variable.

R gravatar imageR ( 2015-08-07 01:04:35 -0500 )edit

Check the posted code, and you can adjust how many characters to strip off the beginning of the string by changing the number of dots in the regexp. Hope that helps!

GregLarkin gravatar imageGregLarkin ( 2015-08-07 12:59:00 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2015-08-06 20:34:03 -0500

GregLarkin gravatar image

updated 2015-08-07 12:58:26 -0500

You can use regexps to extract any tokens you want, as long as it's possible to match those tokens. The presence of a standard delimiter character is not required. It may be harder to write the regexp if your input string varies a lot and less difficult if it has a predictable value.

For instance, this code solves your stated problem, assuming you're trying to split off the 1st 2 characters and the last 4 characters, no matter the length of the input string:

$ cat /tmp/foo.pp
$a = 'mybook'

$b = regsubst($a, '^(..)(.*?)$', '\1')
$c = regsubst($a, '^(.*?)(....)$', '\2')

notice($b)
notice($c)
$ puppet apply /tmp/foo.pp
Notice: Scope(Class[main]): my
Notice: Scope(Class[main]): book
Notice: Compiled catalog for gregorys-mbp in environment production in 0.31 seconds
Notice: Applied catalog in 0.01 seconds
$

Hope that helps!

UPDATE:

Here is some updated code that splits each input string into the first 3 characters and then however many characters are left:

$a = [ 'mybook', 'mybooks', 'mybook123' ]

# Change the number of dots in the 1st matching expression
# depending on how many characters you'd like to place into
# the variable "$b" below. This regexp matches the 1st 3
# characters of the input string.
$regexp = '^(...)(.*?)$'

$a.each |String $str| {
  $b = regsubst($str, $regexp, '\1')
  $c = regsubst($str, $regexp, '\2')

  notice("str = ${str}")
  notice("b = ${b}")
  notice("c = ${c}")
  notice('')
}

And here's the output:

$ puppet apply /tmp/foo.pp
Notice: Scope(Class[main]): str = mybook
Notice: Scope(Class[main]): b = myb
Notice: Scope(Class[main]): c = ook
Notice: Scope(Class[main]):
Notice: Scope(Class[main]): str = mybooks
Notice: Scope(Class[main]): b = myb
Notice: Scope(Class[main]): c = ooks
Notice: Scope(Class[main]):
Notice: Scope(Class[main]): str = mybook123
Notice: Scope(Class[main]): b = myb
Notice: Scope(Class[main]): c = ook123
Notice: Scope(Class[main]):
Notice: Compiled catalog for gregorys-mbp in environment production in 0.35 seconds
Notice: Applied catalog in 0.01 seconds
$
edit flag offensive delete link more

Comments

Thanks Greg, my string varies it is sometimes mybook or mybooks. This $b = regsubst($a, '^(..)(.*?)$', '\1') will give me first 2 characters, how to extract the next 3 or 4 characters depending on what is present? if it is $a= mybooks I need $c=books $a=mybook I need $c=book. Thanks a million again

R gravatar imageR ( 2015-08-07 01:13:20 -0500 )edit

Check the updated code, and you'll see how you can use the same regexp but extract values based on which matching expression you reference. I'm using the iterator to simply pump different strings into the regsubst so you can see how it behaves.

GregLarkin gravatar imageGregLarkin ( 2015-08-07 12:57:23 -0500 )edit

Many thanks Greg, that is exactly what I wanted. I couldn't find this info anywhere on the web, thanks a ton for sharing such a valuable information.

R gravatar imageR ( 2015-08-10 00:12:10 -0500 )edit

Glad to help, and if you get really interested in regular expressions, this is the de facto book: http://shop.oreilly.com/product/9780596528126.do

GregLarkin gravatar imageGregLarkin ( 2015-08-10 11:47:07 -0500 )edit

Ordered the book, thanks Greg.

R gravatar imageR ( 2015-08-12 18:15:30 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-08-06 19:30:29 -0500

Seen: 1,360 times

Last updated: Aug 07 '15