How to chop ruby string having a random number - ruby

My string is - main_string = 'FA117RWD-20 ABC Program (version 10.0)'
In this string I just want to read FA117RWD-20 and in this string version is random which will be coming from somewhere, its version 10 right now it can be version 20 at some other time also the part that I want to read i.e. FA117RWD-20 will also be coming from somewhere randomly so is not of fix length, basically I want to chop everything that is coming after this string. How should I do it?
Thanks

main_string = 'FA117RWD-20 ABC Program (version 10.0)'
main_string[/\S+/]
#=> "FA117RWD-20"
The regular expression matches one or more characters that are not whitespace.
Another way is the following1.
main_string[0..main_string.index(' ')-1]
1 One can use three dots and write main_string[0...main_string.index(' ')], but I prefer to always use two dots.

you can just use #split
'FA117RWD-20 ABC Program (version 10.0)'.split(' ').first

Related

Regex ruby syntax to select a number while excluding specific ones

I am still struggling to find some ruby regex syntax despite the numerous documentation on-line. I have an array of string and I am looking for strings that include one number (whatever the number of digits) but not specific one (let's say for instance dates from 19XX to 201X).
I manage to get the regex for "the line contain a number"
.*\p{N}.*
I manage to get "exclude the line if this number is a year"
(?!19\d\d|20[0-1]\d)\d{4}
But I fail to combine both. I would need something that would intuitively be written as such
(.*\p{N}.*)&&(?!19\d\d|20[0-1]\d)\d{4}
But I am not sure how an AND operator can be used.
Here it is:
^(?!.*19\d\d.*)(?!.*20[01]\d.*)(.*\p{N}.*)$
You want a string that:
(?!.*19\d\d.*) doesn't contains 19xx
(?!.*20[01]\d.*) doesn't contains 200x or 201x
(.*\p{N}+.*) contains, at least, one digit
In regex && means, well, literal && and not and operator
If you want to capture numbers that are not in the range 1900-2019 you can replace with:
(?!\b19\d\d\b)(?!\b20[01]\d\b)(\b\p{N}+\b)
You can test it here
While the solution by Thomas is probably the best one, another option would be to go without negation: just select everything, that matches:
re = /\D(
[03-9]\d*|
(?:1|2|20)(?=\D)|
1[0-8]\d*|
19\d?(?=\D)|
19\d{3,}|
20[2-9]\d*|
20[01]?(?=\D)|
20[01]\d{2,}
)/x
▶ 'Here 2014 and 1945 and 1878 and 20000 and 2 and 19 and 195 and 203'.scan re
#⇒ [["1878"], ["20000"], ["2"], ["19"], ["195"], ["203"]]

ZPL and mixing subsets

we have a new client that needs there bar code created with mixing subset C and A. We are using the ZPL language to print to a zebra printer and I've followed the Zebra programming guide but cant get the output I'm after. I need the bar code to read:
9931265099999891DJS12345670100060020
My code looks like this:
^BY3^BCN,200,Y,N,N
^FD>;9931265099999891>7DJS>512345670100060020^FS
and outputs this with some other characters that are not even ascii:
9931265099999891 S7M &* ...
Can someone tell what I'm doing wrong
thank you
I figured out my own problem....
Thanks Magoo for taking time to look at my question...
When switching to subcode A you cannot just use the letters you want to display but must use a table (in the ZPL programming guide) that shows the characters that represent the characters that need to be displayed. I used this to get it to work, notice after changing to sub-code A (>7) you need duo characters to represent the characters you actually want displayed i.e..
36 = D
42 = J
51 = S
^BY2^BCN,200,Y,N,Y,N
^FD>;9931265099999891>7364251>512345670100060020^FS
Hope my solution helped someone else
cheers all
I got this to work using
^BCN,200,Y,N,N ^FD>;9931265099999891>6DJS1>52345670100060020^FS
Note that this switches to code B instead of A.
The final string of digits is an odd number of characters and it seemed to lop off the final character in code C. The string I constructed uses an even number of digits for each of the code-C sections and the remaining characters in code-B.
I could not get code-A to work at all, but I'm using an old printer (A300) which may not have the latest firmware.

Ruby (on Rails) Regex: removing thousands comma from numbers

This seems like a simple one, but I am missing something.
I have a number of inputs coming in from a variety of sources and in different formats.
Number inputs
123
123.45
123,45 (note the comma used here to denote decimals)
1,234
1,234.56
12,345.67
12,345,67 (note the comma used here to denote decimals)
Additional info on the inputs
Numbers will always be less than 1 million
EDIT: These are prices, so will either be whole integers or go to the hundredths place
I am trying to write a regex and use gsub to strip out the thousands comma. How do I do this?
I wrote a regex: myregex = /\d+(,)\d{3}/
When I test it in Rubular, it shows that it captures the comma only in the test cases that I want.
But when I run gsub, I get an empty string: inputstr.gsub(myregex,"")
It looks like gsub is capturing everything, not just the comma in (). Where am I going wrong?
result = inputstr.gsub(/,(?=\d{3}\b)/, '')
removes commas only if exactly three digits follow.
(?=...) is a lookahead assertion: It needs to be possible to be matched at the current position, but it's not becoming part of the text that is actually matched (and subsequently replaced).
You are confusing "match" with "capture": to "capture" means to save something so you can refer to it later. You want to capture not the comma, but everything else, and then use the captured portions to build your substitution string.
Try
myregex = /(\d+),(\d{3})/
inputstr.gsub(myregex,'\1\2')
In your example, it is possible to tell from the number of digits after the last separator (either , or .) that it is a decimal point, since there are 2 lone digits. For most cases, if the last group of digits does not have 3 digits then you can assume that the separator in front is decimal point. Another sign is the multiple appearance of a separator in big numbers allows us to differentiate between decimal point and separators.
However, I can give a string 123,456 or 123.456 without any sort of context. It is impossible to tell whether they are "123 thousand 456" or "123 point 456".
You need to scan the document to look for clue whether , is used for thousand separator or decimal point, and vice versa for .. With the context provided, then you can safely apply the same method to remove the thousand separators.
You may also want to check out this article on Wikipedia on the less common ways to specify separators or decimal points. Knowing and deciding not to support is better than assuming things will work.

Using Regex to grab multiple values from a string and drop them into an array?

Trying to grab the two $ values and the X value from this string in Ruby/watir:
16.67%: $xxx.xx down, includes the Policy Fee, and x installments of $xxx.xx
So far I've got:
16.67%:\s+\$(\d+.\d{2})
which grabs the first xxx.xx fine, what do I need to add to it to grab the last two variables and load this all into an array?
You can use the following, but regex may be unnecessary if the surrounding text is always the same:
\$(\d+.\d{2}).*?(\d+) installments.*?\$(\d+.\d{2})
http://www.rubular.com/r/sk5wO3fyZF
if you know that the text in between will always be the same you could just:
16.67%:\s+\$(\d+.\d{2}) down, includes the Policy Fee, and x installments of (\d+.\d{2})
You better use scan.
sub(/.*%/, '').scan(/\$?([\d\.]+)/)
Have you considered just splitting the string on the $ character?, then manipulating what you get with a regex or basic string commands?
/\$(\d+.\d{2}).+\$(\d+.\d{2})/ should do it. it wont matter what text is there, only that there are two "$" in the sentence.

how do I pattern match a string within a string and then extract it into a variable

I have come across a problem that I cannot see to solve. I have extracted a line from a web page into a variable. lets say for argument sake this is:
rhyme = "three blind mice Version 6.0"
and I want to be able to first of all locate the version number within this string (6.0) and secondly extract this number into another seperate variable - (I want to specifically extract no more than "6.0")
I hope I have clarified this enough, if not please ask me anything you need to know and I will get back to you asap.
First you need to decide what the pattern for a version number should be. One possibility would be \d+(\.\d+)*$ (a number followed by zero or more (dot followed by a number) at the end of the string).
Then you can use String#[] to get the substring that matches the pattern:
rhyme[ /\d+(\.\d+)*$/ ] #=> "6.0"
You need to use regular expressions. I would use rhyme.scan(/(\d+\.\d+)/) since it can return an array if multiple matches occur. It can also take a block so that you can add range checks or other checks to ensure the right one is captured.
version = "0.0"
rhyme = "three blind mice Version 6.0"
rhyme.scan(/(\d+\.\d+)/){|x| version = x[0] if x[0].to_f < 99}
p version
If the input can be trusted to yield only one match or if you always are going to use the first match you can just use the solution in this answer.
Edit: So after our discussion just go with that answer.
if rhyme =~ /(\d\.\d)/
version = $1
end
The regexp matches a digit, followed by a period, followed by another digit. The parenthesis captures its contents. Since it is the first pair of parenthesis, it is mapped to $1.

Resources