Regex array (["number", "number",...]) - ruby

Can you help me with expression for array: ["232","2323","233"]
I try this: /^\[("\d{1,7}")|(,"\d{1,7}")\]$/
But this expression is not working properly.
I use ruby(rails).

This would validate the array structure well, blocking an entry like [,"123"]
^\[(("\d{1,7}")(,"\d{1,7}")*)?\]$

You may try this ( though it may allow leading ,):
^\[(,?"\d{1,7}")*]$

Try this: /^\[("\d{1,7}")(, ?"\d{1,7}")*\]$/

Related

Wrap all the strings with single quotas

Let's assume that my template is like a following
string1=${obj.firstString}
string2=${obj.secondString}
number1=${obj.firstNumber}
I'm looking for some automatic way to wrap all my string parameters with single quotas? The expected output is
string1='A'
string2='B'
number1=42
I understand that I can write string1=${"'" + obj.firstString + "'"} , but maybe there is some more conventional way for this requirement...
Thanks a lot!
I would just do this:
string1='${obj.firstString}'
string2='${obj.secondString}'
number1=${obj.firstNumber}
It's a template language, so the basic idea is to make your program look similar to its own output.

Xpath expression with OR

I'd like to know if there is a way to verify multiple strings on a Xpath. This is the one I'm using now:
/td[2][text()[contains(.,'Word1')]]
I'd like to do something like this:
/td[2][text()[contains(.,'Word1' OR 'Word2' OR 'Word3')]]
Is that possible?
Updated answer:
I believe, the problem why you are experiencing is case-sensitivity, try writing or in lower-case:
//td[text()[contains(.,'Word1') or contains(.,'Word2') or contains(.,'Word3')]]
If it doesn't help, you can use Union approach:
/td[2][text()[contains(.,'Word1')]] | /td[2][text()[contains(.,'Word2')]] | /td[2][text()[contains(.,'Word3')]]
yes it's possible:
/td[2][text()[contains(.,'Word1') OR contains(.,'Word2') OR contains(.,'Word3')]]
Yes - you just need separate contains() calls:
[contains(., 'Word1') OR contains(., 'Word2') OR contains(., 'Word3')]
As you have it currently, a boolean being passed as the second parameter to contains, rather than a string.
With XPath 2.0 or 3.0 you could also use:
A Quantified Expression to loop over a sequence of words and test if any of the words are contained
//td[2][text()[some $word in ('Word1', 'Word2', 'Word3') satisfies contains(., $word)]]
The matches() function and specify your list of words in a regex:
//td[2][text()[matches(., 'Word1|Word2|Word3')]]

Ruby regex to match all subdomains for my website?

I'm new to using regex expressions. I need to accept all subdomains like:
something.mysite.com
something2.mysite.com
anotherthing.mysite.com
What kind of regex can I put there if I want to do something like:
rack_env['SERVER_NAME'].match <regex>
You shouldn't be using a regex here. The way to go is:
rack_env['SERVER_NAME'].end_with?(".mysite.com")
Something along the lines of \.mysite\.com$ should work. http://rubular.com is a good resource for testing regular expressions.
[a-zA-Z0-9]+\.[a-z]+\.com
something.mysite.com //ok
something2.mysite.com //ok
anotherthing.mysite.com //ok
something2mysite.com //not ok
anotherthing.mysitecom //not ok
But It is risky because you.can.have.as.many.subdomain.as.you.want in the future
If it just the sub domains that are changing you could use:
/\w+\.mysite\.com/

Ruby/Rhomobile: parse directory string representation

I have a string that looks like this: {"whatever-field"=>"gghyduudud"}
I'd like to parse it so that it becomes a hash.
Please help.
Thanks!
You can use eval, but only if the data source is absolutely reliable:
>> eval('{"whatever-field"=>"gghyduudud"}')
=> {"whatever-field"=>"gghyduudud"}
Here is a solution:
dictionary=Hash[*(dict_str[1..dict_str.length-2].split("=>").map {|strval| strval[1..strval.length-2]})]
That will work as long as you want the keys and values as strings. Its a bit long, but it worked for me.

Descendent-or-self in InfoPath

I want to use XPath code in an InfoPath form to sum the data in field12 when field11 is equal to IT. I tried using the following code, but it doesn't work.
number(sum(/descendant-or-self::node()/my:group12[my:field11 = "IT;"]/my:field12))
I suspect this has to do with the multilayering of groups, as shown below. Does anyone know the code that would allow me to get to the data in group12? Thanks in advance for your help.
myfields>group9>group10>group11>group12>field11 field12
Genipro
Looks like:
number(sum(/descendant-or-self::my:group12[my:field11 = 'IT;']/my:field12))
could be right.
decendant-or-self should not be necissary in this case (unless you need the expression to work even if group12 is moved).
This should work fine:
sum(/my:myfields/my:group9/my:group10/my:group11/my:group12[contains(my:field11,'IT')]/my:field12)
It doesn't matter if any of the other groups are repeating either. All group12's will be checked.

Resources