match regular expression - ruby

I have to requirement to check the value 91981552e1775310VgnVCM100000a2b6140a____;standard;212.58.244.70;Oct-22-2012;24353teehdtehg; where the date and 24353teehdtehg is dynamic.
How can I may it more generic so that I can check expected_value =~/actual_value/ excluding the dynamic values in Ruby.

I wouldn't use a regular expression if at all possible. You seem to have an input string that can easily be altered and used to compare against an expected value without using a regular expression.
str = "91981552e1775310VgnVCM100000a2b6140a____;standard;212.58.244.70;Oct-22-2012;24353teehdtehg;"
actual_value = str.split(';')[0..-3].join(';')
# "91981552e1775310VgnVCM100000a2b6140a____;standard;212.58.244.70"
Then just compare the two
expected_value == actual_value

I guess you could use something like :
/91981552e1775310VgnVCM100000a2b6140a____;standard;212\.58\.244\.70;(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}-\d{4};\d{5}[a-z]{9};/
depending on what the string could actually be.

Related

The code always outputs "not"

The following code always outputs "not":
print "input a number please. "
TestNumber = gets
if TestNumber % 2 == 0
print "The number is even"
else
print "The number is not even"
end
What is going wrong with my code?
The gets() method returns an object of type String.
When you call %() on a String object, the return value is a new String object (usually it changes the text. You can read more about string formatting here).
Since there are no String objects that == 0, the if/else will always take the same path.
If you want to use the return value of gets() like a number, you will need to transform it into one first. The simplest approach is probably to use the to_i() method on String objects, which returns a new 'Integer' object. If you're doing something where the user input will not always be an integer (e.g. 3.14 or 1.5), you might need to use a different approach.
One last thing: in your example the result of gets() is saved into a constant called TestNumber. Constants are different to normal variables, and they will probably cause problems if you're not using them intentionally. Normal variables don't start with capital letters. (You can read more about ruby variables here). In ruby you need to write you variable names like this: test_number.
I suspect your Testnumber variable might be interpreted as a string during the operation. make sure the testnum is converted to an integer first even if you put in say 100 it could be its being interpreted as the stirng "100" and not the integer 100.
A similar issue can be found here: Ruby Modulo Division
You have to convert TestNumber from string to integer, as your input has linefeed and/or other unwanted characters that do not match an integer.
Use TestNumber = gets.to_i to convert to integer before testing.

ruby regex not working to remove class name from sql

I have:
BEFORE Gsub sql ::::
SELECT record_type.* FROM record_type WHERE (name = 'Registrars')
sql = sql.gsub(/SELECT\s+[^\(][A-Z]+\./mi,"SELECT ")
AFTER GSUB SQL ::::
SELECT record_type.* FROM record_type WHERE (name = 'Registrars')
The desired result is to remove the "record_type." from the statement:
So it should be :
SELECT * FROM record_type WHERE (name = 'Registrars')
After the regex is run.
I didn't write this, it's in the asf-soap-adaptor gem. Can someone tell me why it doesn't work, and how to fix?
I suppose it should be written like this...
sql = sql.gsub(/SELECT\s+[^\(][A-Z_]+\./mi,"SELECT ")
... as the code in the question won't match if the field name contains _ (underscore) symbol. I suppose that's why this code is in gem: it can work in some conditions (i.e., with underscoreless field names).
Still, I admit I don't understand why exactly this replacement should be done - and shouldn't it include 0-9 check as well (as, for example, 'record_id1' field still won't be matched - and replaced - by the character class in the regular expression; you may have to either expand it, like [0-9A-Z_], or just replace completely with \w).
so your before and after gsubs are the same? I can't tell you why it doesn't work if you dont tell me your expected result. Also for help with interpreting ruby regular expressions check out rubular.com

Whats the XPath equivalent to SQL In query?

I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.

String value formatting

Sometimes in Strings I see something like this %1$s or this %2$d. Can somebody explain to me how to read such things?
Check this document http://download.oracle.com/javase/1,5.0/docs/api/java/util/Formatter.html#syntax the string is basically broken in
%[argument_index$][flags][width][.precision]conversion
From your example %1$s,
% means replace with a parameter
1$ is the position in the parameter array.
s signals that the parameter is a string.
This is taken from Java, but a lot of programming languages use the same syntax for string formatting.
the % stand for the relative argument position, and the "s" or "d" (or others) stands for the type.
This is used to format a string through the printf functions
format= 'The %2$s contains %1$04d monkeys';
printf(format, num, location);
see the printf docs of the langage you use to get all the details (there is a lot)

How to compare string in PL/SQL?

i just want to know that, there is any way through which i can Compare a column value with Some String value.Something like this...
suppose column name is SHIPMENT_EXPEDITE_HAWB then can i have like this in plsql
SHIPMENT_EXPEDITE_HAWB=='PD'
Thaanks in advance!!!
You just need one equals, not two.
IF shipment_expedite_hawb = 'PD' THEN
dbms_output.put_line('Same');
END IF;
Just use the comparison operator =
E.g.
SHIPMENT_EXPEDITE_HAWB = 'PD'

Resources