I'm trying to enter a number into a field that starts with leading zeros. When this number gets entered, the leading zeros are removed.
Expected: 000632 Actual: 632
These are the last 6 digits of a longer number so I need the zero(s) there.
The paramenter that this number is used in has been converted to a string but it's still removing the zeros.
I do have a Transform block in my env file to automatically convert digits to integer:
Transform /^\d+$/ do |number|
number.to_i
end
Although the regex used for my string is ([^"]*/)
It looks like the Transform block is interfering. Is there a way around this? I'm no Regex master :-)
Thanks
Transform blocks are checked for match against every match extracted in your test steps. Because of this the string "000632" matches to the transform you posted and you get the integer extracted instead of the wanted string. If you want to prevent strings of digits beginning with 0 from matching you need to change your transform regex to something like
Transform /^[1-9]\d*$/ do |number|
number.to_i
end
which will then only match strings of digits beginning with 1-9
Related
I need to match a string of variable length(between 5 and 12), composed of uppercase letters and one or more digits between 1 and 8.
How can I specify that I need the whole captured group's length to be between 5 and 12?
I have tried with parenthesis but with no luck.
I have tried this
\s([A-Z]+[1-8]+[A-Z]+){5,12}\s
My idea was to use the quantifier {5,12} to limit the length of the captured group between parenthesis, but clearly it doesn't work like that.
The string needs to be identified inside a normal text just like
"THE STRING I NEED TO DECODE IS SOMETHING LIKE FD1531FHHKWF BUT NOT LIKE g4G58234JJ"
You actually have two conditions to met:
The length of the match is to be specified with curly brackets {5,12}, and before and after there should be not letters/digits. So:
/(?!\b[A-Z]+\b)\b[A-Z1-8]{5,12}\b/
First, we assure that the lookahead for letters only is negative, then we look for the pattern.
Use positive look-ahead on total size of regex
\s(?=^.{5,12}$)([A-Z]+[1-8]+[A-Z]+)\s
Explanation
(?= # look-ahead match start
^.{5,12}$ # 3 to 15 characters from start to end
) # look-ahead match end
I have got a strange sscanf problem with a capital letter 'N'(maybe I do not understand something correct me please):
Example 1:
char cBuff[128];
sscanf("GUIDNameNENE","%*[GUIDName]%127s" ,cBuff);
returns cBuff:ENE
Example 2:
char cBuff[128];
sscanf("GUIDNamenENE","%*[GUIDName]%127s" ,cBuff);
returns cBuff:nENE
Example 3:
char cBuff[128];
sscanf("GUIDNaMENE","%*[GUIDNa]%127s" ,cBuff);
returns cBuff:ENE
I have tried many other variants but still always skips capital N.
Where is the problem?
Thank you in advance!
%[GUIDName] is not a weird way of quoting and matching an exact string. It defines a set of characters that will match. They will match in any order, and they will match repeatedly.
The longest match for the set %[GUIDName] in your input is GUIDNameN.
You could of course say %*[G]%*[U]%*[I]%*[D]%*[N]%*[a]%*[m]%*[e] and that would not eat any of the characters GUIDNam, but it would still eat multiple es.
I would guess the reason it skips the capital N is because it's part of the set of characters that you ignore. The key point is that what you specify between the brackets are a set of characters to match, not in a fixed order, but rather that sscanf tries to match the longest string consisting of only the characters after the '[' up to the first matching ']'. If I recall correct.
You could try specifying the size for the set of characters to be skipped like this:
sscanf("GUIDNameNENE","%*8[GUIDName]%127s" ,cBuff);
But that will of course only work if the string always is eight characters long and if it is you could choose to just ignore the eight initial characters like this:
sscanf("GUIDNameNENE","%*8s%127s" ,cBuff);
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.
Im trying to write a string cleaner that removes underscores, but replaces them with dots when between numbers (ie, when there is a version number in the string).
1_1_OS_And_Network_Specific_Config
I would like this string to come out like
1.1 OS And Network Specific Config
I can replace the underscores easy enough, but im having trouble matching the character between the numbers to replace with the dot.
\d_\d
Seems to match the two digits with the underscore .. but if there is three, like 3.4.1 it doesnt.
Can anyone help?
First, replace the underscores between digits:
subject = subject.gsub(/(?<=\d)_(?=\d)/, '.')
(?<=\d) and (?=\d) are lookaround assertions.
They make sure that there is a digit before ((?<=\d)) and after ((?=\d)) the current location, but they don't actually become part of the match.
Then, remove the rest of the underscores:
subject = subject.gsub(/_/, ' ')
I'm looking to find the first whole occurance of a number within a string. I'm not looking for the first digit, rather the whole first number. So, for example, the first number in: w134fklj342 is 134, while the first number in 1235alkj9342klja9034 is 1235.
I have attempted to use \d but I'm unsure how to expand that to include multiple digits (without specifying how long the number is).
I think, you're looking for this regex
\d+
"Plus" means "one or more". This regex will match all numbers within a string, so pick first one.
strings = ['w134fklj342', '1235alkj9342klja9034']
strings.each do |s|
puts s[/\d+/]
end
# >> 134
# >> 1235
Demo: http://rubular.com/r/YE8kPE2SyW
The easiest way to understand regexes is to think of eachbit is one character; e.g: \d or [1234567890] or [0-9] will match one digit.
To expand this one character you have 2 basic options: * and +
* will match the character 0 or more times
+ will match it one or more times
Like Sergio said you should use \d+ to match many digits.
Excellent tutorial for regexes in general: http://www.regular-expressions.info/tutorial.html