Yaml-Cpp find out if value was quoted - c++

I'm reading in a yaml file with a big map which looks like this:
test_value: '123'
test_value2: 123
test_value3: 1.0
test_value4: true
test_value5: 'some information'
I can parse it to get the values but I want to know the type of the value. I have specified that a double is always written as a double and boolean are always true or false and also that strings have to be always quoted.
Now the problem is that if I want to read it out again with yaml-cpp I don't get the '123' but rather 123 which I then interpret as a int rather then the string it should be.
Is there any possibility that I overlooked to figure out if there where some quotes around it?

I found a way to figure out if it quoted. In a node there is a tag which you can get with node.Tag() this will in my usecase return me a ! or a ? since the ! is only returned on a former string it can be destingueshed like this.

Related

Extracting key-value pairs from a string using ruby & regex

I want to accomplish the following with ruby and if possible a regex:
Input: "something {\"key\":\"value\",\"key2\":3}"
Output: [["\"key\"", "\"value\""], [["\"key2\"", "3"]]
My attempt so far:
s = "something {key:\"value\",key2:3}"
s.scan(/.* {(?:([^:]+):([^,}]+),?)+}$/)
# Output: [["\"key2\"", "3"]]
For some reason the regex above only matches the last key value pair. Does someone know how to retrieve all the pairs?
Just to be clear, "something" can be any kind of string. For this reason, solutions such as (1) splitting the text directly on the equal or (2) a regex as used in s.scan(/(?:([^:]+):([^,}]+),?)/) don't work for me.
I know there are similar questions on SO. Still, from what I saw, they mostly tend towards the solutions 1 & 2 or focus on a single key value pair.
your string looks like a json data structure encoded as a string, you can use JSON.parse for this as long as you remove the word "something " from the string
require 'json'
string = "something {\"key\":\"value\",\"key2\":3}"
# the following line removes the word something
string = string[string.index("{")..-1]
x = JSON.parse(string)
puts x["key"]
puts x["key2"]
you can then convert that to an array if required
alternatively if you want to use regular expressions try
string.scan(/(?:"(\w+)":"?(\w+)"?)/)

How to extract number from value with commas

I'm attempting to extract population from a JSON response from Wikipedia. Here's an example string...
population_estimate=123,456,789<ref> {{ cite...
...but I'm running into issues due to the commas. Specifically, I'd like to extract the number after the =, but the commas are throwing me off. I had originally had an expression that gets everything until <ref> but sometimes that part doesn't appear within the string.
The answer that worked for me came from #ctwheels:
let regexPopulation = "(?<==)\\d{1,3}(?:,\\d{3})*"
let extractedPopulation = matches(for: regexPopulation, in: String)
Note that I had to add the additional \ due to Swift syntax. Otherwise Xcode kept returning:
Invalid escape sequence in literal
Thanks all!

Checking if a string contains a character in Scala

I have a collection of Strings and I'm checking if they're correctly masked or not.
They're in a map and so I'm iterating over it, pulling out the text value and then checking. I'm trying various different combinations but none of which are giving me the finished result that I need. I have gotten it working by iterating over each character but that feels very java-esque.
My collection is something like:
"text"-> "text"
"text"-> "**xt"
"text"-> "****"
in the first two cases I need to confirm that the value is not all starred out and then add them to another list that can be returned.
Edit
My question: I need to check if the value contains anything other an '*', how might I accomplish this in the most efficient scala-esque way?
My attempt at regex also failed giving many false positives and it seems like such a simple task. I'm not sure if regex is the way to go, I also wondered if there was a method I could apply to .contains or use pattern matching
!string.matches("\\*+") will tell you if the string contains characters other than *.
If I understand correctly, you want to find the keys in your map for which the value is not just stars. You can do this with a regex :
val reg = "\\*+".r
yourMap.filter{ case (k,v) => !reg.matches(v) }.keys
If you're not confortable with a regex, you can use a forall statement:
yourMap.filter{ case(k,v) => v.forall(_ == '*') }.keys
Perhaps I misunderstood your question, but if you started with a Map you could try something like:
val myStrings = Map("1"-> "text", "2"-> "**xt", "3"-> "****")
val newStrings = myStrings.filterNot( _._2.contains("*") )
This would give you a Map with just Map(1 -> "text").
Try:
val myStrings = Map("1"-> "text", "2"-> "**xt", "3"-> "****")
val goodStrings = myStrings.filter(_._2.exists(_ !='*'))
This finds all cases where the value in the map contains something other than an asterisk. It will remove all empty and asterisk-only strings. For something this simple, i.e. one check, a regex is overkill: you're just looking for strings that contain any non-asterisk character.
If you only need the values and not the whole map, use:
val goodStrings = myStrings.values.filter(_.exists(_ !='*'))

Regex for both double precision value or empty string: ^[0-9]\d*(\.\d+)?$

For validation if a text is a double precision value, I use ^[0-9]\d*(\.\d+)?$. However, what regex should I use so that either an empty string or a double precision value would match?
You mean as in empty string?
You could use this:
^([0-9]\d*(.\d+)?|)$
Though to make it work as intended, you probably want:
^([0-9]+(\.[0-9]+)?|)$
or
^(\d+(\.\d+)?|)$
Notice I put an or operator | there and since there's nothing after it, it will match an empty line.
This should work
^\d*(?:\d\.\d+)?$
It will match strings like:
'123'
'123.4'
'0.3'
''
It will not allow strings start with a decimal point (e.g. .3); if you'd like allow that as well, use this:
^\d*(?:\.\d+)?$
If you'd also like allow strings that end with a decimal point (e.g. 3.), use this:
^\d*(?:\.\d*)?$
You could also do this
^\d+([.]\d+)?$|^$

R grepl to find a pure number

Probably a very basic question but its buggging me that i can't easily find a solution...so i thought i should come to the wisdom of the SO wise ones...
I would like to be able to return a TRUE or FALSE acording to if a character string is a pure number rather than just containing numbers... The closest I got was
grepl("[0-9]","99393")
grepl("[0-9]","blah")
However this doesn't work since the following is returned as TRUE when it should be FALSE
grepl("[0-9]","993T3")
As ever any help would be appreciated!
EDIT
As joran pointed out it is important to note that the character string will only ever include integers and letters, i.e. will not include decimal points or commas for the number...
You should specify the whole regular expression and specify the beginning (^) and the end of the string ($). For instance :
> grepl("^[[:digit:]]+$","993T3")
[1] FALSE
Look at http://en.wikibooks.org/wiki/R_Programming/Text_Processing#Regular_Expressions if you want to learn more about regexp.
Why don't you just use robust internal methods for coercing to either an integer or numeric?
It will return NA if it can't be done. Use is.na if you want a logical result:
is.na( as.integer( "993T3" ) )
# [1] TRUE
is.na( as.integer( "99393" ) )
# [1] FALSE
Remember that if you are dealing with floating point numbers use as.numeric otherwise you will truncate the floating point part of your number using as.integer
What about !grepl("[^0-9]","993T3")?
Edit: This returns TRUE for the empty string. To avoid this, use
!grepl("[^0-9]", x) & nzchar(x)
for a vector x of character type.