GREP: how to match positive or negative digit? - grep

I would to apply a text style on a percentage (%) number according to is polarity.
Like, if I get a -21% so I would Indesign apply the style1, if it's 21%, ID must apply another style.
This is my starting point to match content:
\.^-\d+\K% for negative digit
^+\+\d+\K% for positive
If someone can help me, it's my first time using GREP style in ID.
Thanks

For positive values use
^[+]?[0-9]+%$
For negative values, use
^-[0-9]+%$
Details
^ - start of string
[+]? - an optional + char
- - a - char
[0-9]+ - 1 or more digits
% - a % symbol
$ - end of string.

Related

How do I remove point symbol from the decimal number?

I'm trying to take decimal number as an input and I need output of all numbers but without the point symbol in it.
Example input: 123.4
Wanted output 1234
The problem I have that when converting decimal number into string and trying to remove "." using :gsub('%.', '') its removing the point symbol but outputs 1234 1 .
I have tried :gsub('.', '') as well but it outputs 5.
I'm clueless where those numbers come from, here is the screenshot:
Use this syntax to get what you want and discard/ignore what you dont need...
local y = 123.4
-- Remove decimal point or comma here
local str, matches = tostring(y):gsub('[.,]', '')
-- str holds the first return value
-- The second return value goes to: matches
-- So output only the string...
print(str) -- Output: 1234
-- Or/And return it...
return str
There are two issues at play here:
string.gsub returns two values, the resulting string and the number of substitutions. When you pass the results of gsub to print, both will be printed. Solve this by either assigning only the first return value to a variable (more explicit) or surrounding gsub with parenthesis.
. is a pattern item that matches any character. Removing all characters will leave you with the empty string; the number of substitutions - 5 in your example - will be the number of characters. To match the literal dot, either escape it using the percent sign (%.) or enclose it within a character set ([.]), possibly adding further decimal separators ([.,] as in koyaanisqatsi's answer).
Fixed code:
local y = 123.4
local str = tostring(y):gsub("%.", "") -- discards the number of substitutions
print(str)
this is unreliable however since tostring guarantees no particular output format; it might as well emit numbers in scientific notation (which it does for very large or very small numbers), causing your code to break. A more elegant solution to the problem of shifting the number such that it becomes an integer would be to multiply the number by 10 until the fractional part becomes zero:
local y = 123.4
while y % 1 ~= 0 do y = y * 10 end
print(y) -- note: y is the number 1234 rather than the string "1234" here

Make a pattern in lua that match one parenthese and string

I want to make a pattern that matches strings like (figure.
I tried
string.find("See this example (figure 1), "%(%figure$")
But it doesn't work.
Your %(%figure$ pattern is invalid, it throws
missing '[' after '%f' in pattern
because %f defines a frontier pattern.
You may use
string.match("See this example (figure 1)", "%((figure%s*%d+)%)")
See Lua demo online
Details
%( - a ( char
(figure%s*%d+) - Capturing group (this value will be the output of string.match): figure, zero or more whitespaces (%s*) and then 1+ digits (%d+)
%) - a ) char

Regular wrong regular expression, not validating

please i want to validate the inputs from a user, the format for the inputs would be: 3 uppercase characters, 3 integer numbers, an optional space, a -, an optional space, either a 'LAB or ((EN or ENLH) with 1 interger number ranging from a [1-9]).
The regex i wrote is
/\D{3}\d{3}\s?-\s?(LAB|(EN(LH)?\d{1}))/
am finding it difficult to stop inputs after the LAB so that when EEE333 - LAB1 is inputed it becomes invalid.
If you are asking how to prevent LAB1 at the end, use an end of line anchor $ in your regex test:
/\D{3}\d{3}\s?-\s?(LAB|(EN(LH)?\d{1}))$/
If you are trying to require exactly one digit at the end of the acceptable strings, move the single digit match outside of the optional groups:
/\D{3}\d{3}\s?-\s?(LAB|(EN(LH)?))\d{1}$/
I have wrote for you the following regular expression:
[A-Z]{3}[0-9]{3}\s?-\s?(?:LAB|(?:EN|LH))[1-9]{1}
The regex works a follows:
[A-Z]{3}
MATCH EXACTLY THREE UPPERCASE CHARACTERS RANGING FROM A TO Z
[0-9]{3}
MATCH EXACTLY THREE NUMBERS RANGING FROM 0 TO 9
\s?\-\s?
MATCH a space (optional) or a '-' (required) or a space (optional)
(?:LAB|(?:EN|LH))
MATCH 'LAB' OR ('EN' OR 'LH')?: omits capturing LAB OR EN OR LH
[1-9]{1}
MATCH EXACTLY ONE NUMBERS RANGING FROM 1 TO 9
You could place your regex between word boundaries \b.
You start your regex with \D which is any character that is not a digit. That would for example also match $%^. You could use [A-Z].
You use \d{1} which is a shorhand for [0-9], but you want to match a digit between 1 and 9 [1-9]. You could also omit the {1}.
Maybe this updated will work for you?
\b[A-Z]{3}\d{3} ?- ?(?:LAB|(?:EN(?:LH)?[1-9]))\b
Explanation
A word boundary \b
Match 3 uppercase characters [A-Z]{3}
Match 3 digits \d{3}
Match an optional whitespace, a hyphen and another optional whitespace ?- ?
A non capturing group which for example matches LAB or EN EN1 or ENLH or ENLH9 (?:EN(?:LH)?[1-9]))
A word boundary \b

Using regex to grab only digits or the decimal separator

I have the following piece of code to grab the amount and another amount.
#payer_contract_params['payer'] = JSON.parse(#payer_contract_params['payer'])
#payer_contract_params['amount'] = #payer_contract_params['amount'].to_s.tr('$', '').tr(',','')
#payer_contract_params['stoploss_amount'] = #payer_contract_params['stoploss_amount'].to_s.tr('$', '').tr(',','')
It works, but it will only work in locales which use '$' as the currency and ',' as a separator. How could I Use regex to grab grab only digits or the decimal separator?
Use gsub like this #payer_contract_params['amount'].to_s.gsub(/[^\d,]/, '')
This will replace all characters that are not digits or comma.
It's simple
/(^\d+$)|(^\.$)/
Breakdown
() - Indicates a capturing group
^ - Indicates the regex starts with the following expression
\d - Matches any digit
+ - Mathes 1 or more of the previous selector
$ - Indicates the regex ends with the previous expression
| - Or
\. - Matches a period. Note the slash to escape it.
You can calculate your regex's in Ruby here at http://rubular.com/

print floats in ada

I want to print a float number, i am using the package FLOAT_IO.
Is there a way to control the number of digits after the dot and before it?
The procedure Put in Ada.Float_Text_IO has three optional format-controlling parameters Fore, Aft, Exp that control the output. The meaning of these parameters is as follows:
Fore denotes the number of digits (including the possible negative sign and leading zeros) before the dot;
Aft denotes the number of digits after the dot (including any trailing zeros);
Exp denotes the number of digits of the exponent (if necessary).
For a more thorough description and the default values of the format-controlling parameters see the Ada 95 Reference Manual, section A.10.9.

Resources