I need a regular expression that will find all the numbers on a sentence.
For example:
"I have 3 bananas and 37 balloons"
I will get:
3
37
"The time is 20:00 and I have 7 tanks"
I will get:
20
00
7
Split your string by [^0-9]+.
JAVA: String[] numbers = "yourString".split("[^0-9]+");
JavaScript: var numbers = "yourString".split(/[^0-9]+/);
PHP: $numbers = preg_split("/[^0-9]+/", "yourString");
The regex itself is as simple as \d+, but you will also need to set a flag to match it globally, the syntax of which depends on the programming language or software you are using.
EDIT: Some examples:
Python:
import re
re.findall(r"\d+", my_string)
JavaScript:
myString.match(/\d+/g)
The regex you are looking for is [0-9]+ or \d+. You should then get multiple matches for the sentence.
Related
I have a set of strings that have some letters, occasional one number, and then somewhere 2 or 3 numbers. I need to match those 2 or 3 numbers.
I have this:
\w*(\d{2,3})\w*
but then for strings like
AAA1AAA12A
AAA2AA123A
it matches '12' and '23' respectively, i.e. it fails to pick the three digits in the second case.
How do I get those 3 digits?
Here is how you would do it in Java.
the regex simply matches on a group of 2 or 3 digits.
the while loop uses find() to continue finding matches and the printing the captured match. The 1 and the 1223 are ignored.
String s= "AAA1AAA12Aksk2ksksk21sksksk123ksk1223sk";
String regex = "\\D(\\d{2,3})\\D";
Matcher m = Pattern.compile(regex).matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
prints
12
21
123
Looks like the correct answer would be:
\w*?(\d{2,3})\w*
Basically, making preceding expression lazy does the job
I have a list of strings each telling me after how many iterations an algorithm converged.
string_list = [
"Converged after 1 iteration",
"Converged after 20 iterations",
"Converged after 7 iterations"
]
How can I extract the number of iterations? The result woudl be [1, 20, 7]. I tried with regex. Apparently (?<=after )(.*)(?= iteration*) will give me anything in between after and iteration but then this doesn't work:
occursin(string_list[1], r"(?<=after )(.*)(?= iteration*)")
There's a great little Julia package that makes creating regexes easier called ReadableRegex, and as luck would have it the first example in the readme is an example of finding every integer in a string:
julia> using ReadableRegex
julia> reg = #compile look_for(
maybe(char_in("+-")) * one_or_more(DIGIT),
not_after = ".",
not_before = NON_SEPARATOR)
r"(?:(?<!\.)(?:(?:[+\-])?(?:\d)+))(?!\P{Z})"
That regex can now be broadcast over your list of strings:
julia> collect.(eachmatch.(reg, string_list))
3-element Vector{Vector{RegexMatch}}:
[RegexMatch("1")]
[RegexMatch("20")]
[RegexMatch("7")]
To extract information out of a regex, you want to use match and captures:
julia> convergeregex = r"Converged after (\d+) iteration"
r"Converged after (\d+) iteration"
julia> match(convergeregex, string_list[2]).captures[1]
"20"
julia> parse.(Int, [match(convergeregex, s).captures[1] for s in string_list])
3-element Vector{Int64}:
1
20
7
\d+ matches a series of digits (so, the number of iterations here), and the parantheses around it indicates that you want the part of the string matched by that to be placed in the results captures array.
You don't need the lookbehind and lookahead operators (?<=, ?=) here.
I want to get number in string at specific position, and i cant do this.
example:
STRING:
180 MATTHEW SANDLER DON 30.00 1.361,67 00
181 JOHN 30.00 5.987,00 99
182 LUCY P. 30.00 3.888,98 71
I want to return on each line just the numbers:
1.361,67
5.987,00
3.888,98
Unfortunately the name has a variable number of spaces, otherwise it would be a simple string.Split(' ') problem
Does anyone know how to do it, please?
The following pattern should match the values in your example:
\b\S*,\d+\b
Example:
http://rextester.com/LZVQN62207
If we conceptually define the term you want to match as being the last term before the final two (or more?) numbers at the end of each line, then we can use the following regex pattern:
(\d+\.\d+,\d+) \d+$
The quantity in parenthesis will be captured and made available after the regex has run in C#.
string input = "180 MATTHEW SANDLER DON 30.00 1.361,67 00";
var groups = Regex.Match(input,#"(\d+\.\d+,\d+) \d+$").Groups;
var x1 = groups[1].Value;
Console.WriteLine(x1);
Demo here:
Rextester
Hello I should think of this regular expression:
The telephone number should begin with 087 OR 088 OR 089 and then it should be followed by7 digits:
This is what I made but it doesn't work correctly: it accepts only numbers which begin with 089
(087)|(088)|(089)[0-9]{7}";
/08[789]\d{7}/
that will match 087xxxxxxx, 088xxxxxxx, 089xxxxxxx numbers.
See it in action
Maybe /08[7-9][0-9]{7}/ is what you're searching for?
Autopsy:
08 - a literal 08
[7-9] - matches the numbers from 7-9 once
[0-9]{7} - matches the numbers from 0-9 repeated exactly 7 times
That said, you might prefer /^08[7-9][0-9]{7}$/ if your string is only the phone number. (^ means "the string MUST start here" and $ means "the string MUST end here").
Actually that will be far better regex for Bulgarian phone numbers:
/(\+)?(359|0)8[789]\d{1}(|-| )\d{3}(|-| )\d{3}/
It checks:
Phones that start with country code(+359) or 0 instead;
if the phone number use delimiters like - or space.
I tried it in https://regex101.com and it did not work against my test set. So I tweaked it a little bit with the below regex pattern:
^([+]?359)|0?(|-| )8[789]\d{1}(|-| )\d{3}(|-| )\d{3}$
How I use regular expression in order to check if my number have different digits.
For example: the number: 5554 is ok but the number: 5555 is not ok.
This regex checks if all the digits are the same. Then take the opposite and you will find if your number has different digits.
^(\d)\1*$
This regex checks if at least one digit is different:
(\d)((?!\1)\d)+
EDIT in C# with RegularExpression Attribute
[RegularExpression(#"(\d)((?!\1)\d)+")]
In perl for any digits:
/^(\d)\1*$/
Some tests:
Content of script.pl:
use warnings;
use strict;
while ( <DATA> ) {
print unless /^(\d)\1*$/;
}
__DATA__
55555
56
5556
56565
5
6555
55655
55
Running the script in a linux machine:
perl script.pl
Result:
56
5556
56565
6555
55655
Don't use regular expressions for that.
Four digits which are not the same:
([0-9])(?!\1\1\1)[0-9]{3}
Backreferences in a negative lookahead don't work in some regex implementations. I tried this regex with grep -P.
var a = 5555;
var b = 5554;
var a1 = a.ToString().ToCharArray().Distinct().Count();
var b1 = b.ToString().ToCharArray().Distinct().Count();
And then check for count.