string:
ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd
substring that i want to get:
https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C
i tried this but doesn't work
print (log.split("ecsdcsdcsdfvdfv",1)[1])
You can try this
log.split('":"')[1].split('","')[0]
But this is not the best way to do what you are trying to achieve. Better parse it and get what you want.
This yield the expected result.
The way you used .split() was wrong, you did not include the " character.
string = 'ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd'
substring = string.split('ecsdcsdcsdfvdfv":"')[1].split('","vsvdfvsfvcfvfdcvfd')[0]
If you want to get the string between the ":" and ",", then you can use regular expression to do it.
re.match('(.*\":\")([^\",\"]*)(\",\".*)', log).group(2)
Given your input
'ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd'
you will get
'https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C'
And if you input something like
'ecsdcsdcs":"dfvdfv":"https://s...F0A0C","vsvdfvsfvcfvfdc","vfd'
you will get
'https://s...F0A0C'
Dont forget to import re.
Related
I am fairly new to Puppet and Ruby. Most likely this question has been asked before but I am not able to find any relevant information.
In my puppet code I will have a string variable retrieved from the fact hostname.
$n="$facts['hostname'].ex-ample.com"
I am expecting to get the values like these
DEV-123456-02B.ex-ample.com,
SCC-123456-02A.ex-ample.com,
DEV-123456-03B.ex-ample.com,
SCC-999999-04A.ex-ample.com
I want to perform the following action. Change the string to lowercase and then replace the
-02, -03 or -04 to -01.
So my output would be like
dev-123456-01b.ex-ample.com,
scc-123456-01a.ex-ample.com,
dev-123456-01b.ex-ample.com,
scc-999999-01a.ex-ample.com
I figured I would need to use .downcase on $n to make everything lowercase. But I am not sure how to replace the digits. I was thinking of .gsub or split but not sure how. I would prefer to make this happen in a oneline code.
If you really want a one-liner, you could run this against each string:
str
.downcase
.split('-')
.map
.with_index { |substr, i| i == 2 ? substr.gsub(/0[0-9]/, '01') : substr }
.join('-')
Without knowing what format your input list is taking, I'm not sure how to advise on how to iterate through it, but maybe you have that covered already. Hope it helps.
Note that Puppet and Ruby are entirely different languages and the other answers are for Ruby and won't work in Puppet.
What you need is:
$h = downcase(regsubst($facts['hostname'], '..(.)$', '01\1'))
$n = "${h}.ex-ample.com"
notice($n)
Note:
The downcase and regsubst functions come from stdlib.
I do a regex search and replace using the regsubst function and replace ..(.)$ - 2 characters followed by another one that I capture at the end of the string and replace that with 01 and the captured string.
All of that is then downcased.
If the -01--04 part is always on the same string index you could use that to replace the content.
original = 'DEV-123456-02B.ex-ample.com'
# 11 -^
string = original.downcase # creates a new downcased string
string[11, 2] = '01' # replace from index 11, 2 characters
string #=> "dev-123456-01b.ex-ample.com"
I have a string in excel that I need to extract a substring from
This is an example of the string:
<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>
I'm new to regex and powershell, but I'm trying to find a way to extract the "hostname here" portion of the string. It's variable length, so indexing won't be reliable.
since you changed the sample, the comment code i posted won't work. [grin] this will, tho ...
$InStuff = '<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>'
$InStuff.Split(':')[-1].Split('<')[0].Trim()
output = hostnamehere
if you have a set of sample strings, then you likely otta post them so the code can be arranged to handle the needed variants.
If that were xml, it would be straightforward
[xml]$xml = '<Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue></Text>'
(-split $xml.text.textvalue)[1]
hostnamehere
Something like that is coming in:
str="Hello;this;is;a;text"
What I do want as result is this:
result="72:101:108:108:111;116:104:105:115;..."
which should be the Text in ASCII.
You could use string matching to get each word separated by ; and then convert, concat:
local str = "Hello;this;is;a;text"
for word in str:gmatch("[^;]+") do
ascii = table.pack(word:byte(1, -1))
local converted = table.concat(ascii, ":")
print(converted)
end
The output of the above code is:
72:101:108:108:111
116:104:105:115
105:115
97
116:101:120:116
I'll leave the rest of work to you. Hint: use table.concat.
Here is another approach, which exploits that fact that gsub accepts a table where it reads replacements:
T={}
for c=0,255 do
T[string.char(c)]=c..":"
end
T[";"]=";"
str="Hello;this;is;a;text"
result=str:gsub(".",T):gsub(":;",";")
print(result)
Another possibility:
function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
Finding certain character or string (such as ";") can be done by using string.find - https://www.lua.org/pil/20.1.html
Converting character to its ASCII code can be done by string.byte - https://www.lua.org/pil/20.html
What you need to do is build a new string using two functions mentioned above. If you need more string-based functions please visit official Lua site: https://www.lua.org/pil/contents.html
Okay...I got way further, but I can't find how to return a string made up of two seperate strings like
str=str1&" "&str2
I'm trying to read a string in a specific format
RealSociedad
this is one example of string and what I want to extract is the name of the team.
I've tried something like this,
houseteam = sscanf(str, '%s');
but it does not work, why?
You can use regexprep like you did in your post above to do this for you. Even though your post says to use sscanf and from the comments in your post, you'd like to see this done using regexprep. You would have to do this using two nested regexprep calls, and you can retrieve the team name (i.e. RealSociedad) like so, given that str is in the format that you have provided:
str = 'RealSociedad';
houseteam = regexprep(regexprep(str, '^<a(.*)">', ''), '</a>$', '')
This looks very intimidating, but let's break this up. First, look at this statement:
regexprep(str, '^<a(.*)">', '')
How regexprep works is you specify the string you want to analyze, the pattern you are searching for, then what you want to replace this pattern with. The pattern we are looking for is:
^<a(.*)">
This says you are looking for patterns where the beginning of the string starts with a a<. After this, the (.*)"> is performing a greedy evaluation. This is saying that we want to find the longest sequence of characters until we reach the characters of ">. As such, what the regular expression will match is the following string:
<ahref="/teams/spain/real-sociedad-de-futbol/2028/">
We then replace this with a blank string. As such, the output of the first regexprep call will be this:
RealSociedad</a>
We want to get rid of the </a> string, and so we would make another regexprep call where we look for the </a> at the end of the string, then replace this with the blank string yet again. The pattern you are looking for is thus:
</a>$
The dollar sign ($) symbolizes that this pattern should appear at the end of the string. If we find such a pattern, we will replace it with the blank string. Therefore, what we get in the end is:
RealSociedad
Found a solution. So, %s stops when it finds a space.
str = regexprep(str, '<', ' <');
str = regexprep(str, '>', '> ');
houseteam = sscanf(str, '%*s %s %*s');
This will create a space between my desired string.
Is there a nice way to extract part of a string with linq, example:
I have
string s = "System.Collections.*";
or
string s2 = "System.Collections.Somethingelse.*";
my goal is to extract anything in the string without the last '.*'
thankx I am using C#
The simplest way might be to use String.LastIndexOf followed by String.Substring
int index = s.LastIndexOf('.');
string output = s.Substring(0, index);
Unless you have a specific requirement to use LINQ for learning purposes of course.
You might want a regex instead. (.*)\.\*
With the regex:
string input="System.Collections.Somethingelse.*";
string output=Regex.Matches(input,#"\b.*\b").Value;
output is:
"System.Collections.Somethingelse"
(because "*" is not a word) although a simple
output=input.Replace(".*","");
would have worked :P