This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Find shortest matches between two strings
(4 answers)
Closed 5 years ago.
I'd like to match if x > 64 then return 0, 0 end inside this string.
function if x > 64 then return 0, 0 end return 1, 0 end
I'm using if(.*)then(.*)end. However, this matches: if x > 64 then return 0, 0 end return 1, 0 end, which is one end too many.
This should do:
if(.*)then(.*?)end
You could add word boundaries to make sure you match end as a word, not as a part of a variable name:
if(.*)then(.*?)\bend\b
To analyse code, you probably should use a parser instead of a regex, though.
Related
This question already has answers here:
Regular expression works on regex101.com, but not on prod
(1 answer)
What does std::match_results::size return?
(3 answers)
Closed 1 year ago.
I'm trying to extract all numbers (positive and negative) with 4 to 5 digits from a string. To do so, I'm using the following code:
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string test_string("S: 0 2 -1500 -1500 0 -10000 10000");
std::smatch match_results;
std::regex rgx("-?[0-9]{4,5}");
std::regex_search(test_string,match_results,rgx);
std::cout<<match_results.str(0)<<' '<<match_results.str(1)<<' '<<std::endl;
return 0;
}
I would expect this code to output
-1500 -1500
Instead the output is only
-1500
Outputting the 3rd and 4th match doesn't work either.
Testing the regular expresion on the exact same teststring on regex101.com yields all for numbers with more than 3 digits.
What am I doing wrong here?
This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});
I'm currently writing a validator where I need to check the formats of floats. My code reads in a format of (x,y) where x is the total possible digits in the float and y is the maximum digits out of x that can be after the decimal point. Apologies if this question has already been answered before, but I wasn't able to find anything similar.
For example, given a format of (5,3):
Valid values
55555
555.33
55.333
5555.3
.333
Invalid values
55555.5
555555
5.5555
.55555
This is my first time working with regex so if you guys have any tutorials that you recommend, please send it my way!
You can use a lookahead to ensure both conditions, like
^(?=(?:\.?\d){1,5}$)\d*(?:\.\d{1,3})?$
^ match from the start of the string
(?=(?:\.?\d){1,5}$) check for the presence of 1 up to 5 digits to the end of the string - not caring to much about the correct number of dots
\d* match any number of digits
(?:\.\d{1,3})? match up to 3 decimal places
$ ensure end of the string
See https://regex101.com/r/lrP56w/1
Assuming JS you can try
function validate(value, total, dec) {
let totalRegex = new RegExp(`\\d{0,${total}}$`);
let decimalRegex = new RegExp(`\\.\\d{0,${dec}}$`);
return totalRegex.test(value.replace(".","")) && (!(/\./.test(value)) || decimalRegex.test(value));
}
console.log(validate("555.55", 5, 2));
console.log(validate("55.555", 5, 2));
console.log(validate("5.5", 5, 2));
console.log(validate("55555", 5, 2));
console.log(validate("5.5555", 5, 2));
This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?
We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Exception when comparing an (int)double and (int)int
IGNORE THIS POST. ACCIDENTLY REPOSTED
Sorry
if((int)time >= 600){ time_s.insert(4, sec);
should be,
if((int)time >= 600){ time_s.insert(3, sec); // digit 3 instead of 4
From your code, I suppose the string size is 6 characters (0 to 4 and 5th character as nul). Inserting 2 digit at 4th position and 5th position would overwrite nul.
Do cross verify, as I have made a guess seeing your code.