following string:
23434 5465434
58495 / 46949345
58495 - 46949345
58495 / 55643
d 44444 ssdfsdf
64784
45643 dfgh
58495/55643
48593/48309596
675643235
34565435 34545
it only want to extract the bold ones. its a five digit number(german).
it should not match telephone numbers 43564 366334 or 45433 / 45663,etc as in my example above.
i tried something like ^\b\d{5} but thats not a good beginning.
some hints for me to get this working?
thanks for all hints
You could add a negative look-ahead assertion to avoid the matches with phone numbers.
\b[0124678][0-9]{4}\b(?!\s?[ \/-]\s?[0-9]+)
If you're using Ruby 1.9, you can add a negative look-behind assertion as well.
You haven't specified what distinguishes the number you're trying to search for.
Based on the example string you gave, it looks like you just want:
^(\d{5})\n
Which matches lines that start with 5 digits and contain nothing else.
You might want to permit some spaces after the first 5 digits (but nothing else):
^(\d{5})\s*\n
I'm not completely sure about the specified rules. But if you want lines that start with 5 digits and do not contain additional digits, this may work:
^(\d{5})[^\d]*$
If leading white space is okay, then:
^\s*(\d{5})[^\d]*$
Here is the Rubular link that shows the result.
^\D*(\d{5})(\s(\D)*$|()$)
This should (it's untested) match:
line starting with five digits (or some non-digits and then five digits), then
a space, and ending with some non-numbers
line starting and ending with five
digits (or some non-digits and then five digits)
\1 would be the five digits
\2 would be the whole second half, if any
\3 would be the word after the digits, if any
edited to fit the asker's edited question
edit again: I came up with a much more elegant solution:
^\D*(\d{5})\D*$
Related
I have a special use case where I want to discard all the contractions from the string and select only words followed by alphabets which do not contain any special character.
For eg:
string = "~ ASAP ASCII Achilles Ada Stackoverflow James I'd I'll I'm I've"
string.scan(/\b[A-z][a-z]+\b/)
#=> ["Achilles", "Ada", "Stackoverflow", "James", "ll", "ve"]
Note: It's not discarding the whole word I'll and I've
Can someone please help how to discard the whole word which contains contractions?
Try this Regex:
(?:(?<=\s)|(?<=^))[a-zA-Z]+(?=\s|$)
Explanation:
(?:(?<=\s)|(?<=^)) - finds the position immediately preceded by either start of the line or by a white-space
[a-zA-Z]+ - matches 1+ occurrences of a letter
(?=\s|$) - The substring matched above must be followed by either a whitespace or end of the line
Click for Demo
Update:
To make sure that not all the letters are in upper case, use the following regex:
(?:(?<=\s)|(?<=^))(?=\S*[a-z])[a-zA-Z]+(?=\s|$)
Click for Demo
The only thing added here is (?=\S*[a-z]) which means that there must be atleast one lowercase letter
I know that there's an accepted answer already, but I'd like to give my own shot:
(?<=\s|^)\w+[a-z]\w*
You can test it here. This regex is shorter and more efficient (157 steps against 315 from the accepted answer).
The explanation is rather simple:
(?<=\s|^)- This is a positive look behind. It means that we want strings preceded by a whitespace character or the start of the string.
\w+[a-z]\w* - This one means that we want strings composed by letters only (word characters) containing least one lowercase letter, thus discarding words which are whole uppercase. Along with the positive look behind, the whole regex ends up discarding words containing special characters.
NOTE: this regex won't take into account one-letter words. If you want to accomplish that, then you should use \w*[a-z]\w* instead, with a little efficiency cost.
I want to make a REGEX for a phone number validation that only allows:
7 digits (5557865) that's formatted exactly like the example.
I'm pretty unfamiliar with regex or else I would tackle this myself. Hopefully that is enough info let me know if you need anything else.
Working example https://regex101.com/r/oZ2yQ0/1
\(\d{7}\)
\( matches the character ( literally
\d{7} match a digit [0-9]
Quantifier: {7} Exactly 7 times
\) matches the character ) literally
If it is just to match 7 digit number, you can use \d{7} or [0-9]{7}.
can be just with
/\d{7}/ #or
/\d{1..7}/ #or
/\d[0-9]{7}/
the \d matches digits and {7} the number of the digits.
There's a few ways to tackle this:
# Ensure the number consists entirely of seven digits, nothing else.
number.match(/\A\d{7}\z/)
# Remove all non-digit characters (\D) and test that the length is 7.
number.gsub(/\D/, '').length == 7
# Test that this is either NNN-NNNN or NNNNNNN.
number.match(/\A\d\d\d\-\d\d\d\d\z/)
Normally you want to make your validation methods as lenient as possible while still ensuring things are valid.
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.
I am trying to write a regex in Ruby to search a string for numbers of only four digits. I am using
/\d{4}/ but this is giving me number with four and more digits.
Eg: "12345-456-6575 some text 9897"
In this case I want only 9897 and 6575 but I am also getting 1234 which has a length of five characters.
"12345-456-6575 some text 9897".scan(/\b\d{4}\b/)
=> ["6575", "9897"]
Try matching on a word boundary (\b) on both sides of the four digit sequence:
s = '12345-456-6575 some text 9897'
s.scan(/\b\d{4}\b/) # => ["6575", "9897"]
You have to add one more condition to your expression: the number can only be returned if there are 4 digits AND both the character before and after that 4-digit number must be a non-number.
or even more generally: anything but a digit before and/or after the four digits:
/\D\d{4}\D/
Try /[0-9][0-9][0-9][0-9][^0-9]/
You should specify a separator for the pattern. As in if the digits would be preceded and followed by a space the REGEX would /\s\d{4}\s/, hope that helps.
I need a regex for a password which meets following constraints in my rails project:
have a minimum of 8 and a maximum of 16 characters
be alphanumeric only
contain at least one letter and one number.
My current regex is:
/^(?=.*\d)(?=.*([a-z]|[A-Z])).{8,16}$/
This allows me all the restrictions but the special characters part is not working. What is it that I am doing wrong. Can someone please correct this regex?
Thanks in advance.
/^(?=.*\d)(?=.*[a-zA-Z])[0-9a-zA-Z]{8,16}$/
The last part of your regex, .{8,16}, allows any character with a dot.
The lookahead only makes sure that there's at least one digit and one letter - it doesn't say anything about other characters. Also, note that I've updated your letter matching part - you don't need two character classes.
Disallowing special characters in a password is totally counter intuitive. Why are you doing that?