Check for format X-3 - preg-match

Can anyone help me figure out where I am going wrong.
I am trying to check for the start of the string to be 1+ alpha chars followed by a dash and then 1+ numbers
So x-2 X-2 XX-2 XX-22 xx-22 Xx-2 any of these would pass. It must contain a dash preceded by letters and followed by numbers...
It would also have to pass Xx-2x but NOT Xx-x3 or 3x-33
There will NEVER be a number in front of the dash or a letter as the first character just after the dash.
This is what I have but doesn't seem to work
preg_match('/^([a-zA-Z]+[-])+[0-9]+$/',$str)

Related

Discard contractions from string

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.

Regex incorrectly matching punctuation (including spaces)

I am trying to check if a string contains at least one lowercase letter, uppercase letter, and a number, but not punctuation (including spaces).
For example
4aBc8Fk3 should match
4aBc 8.;3 should not match
I tried the following, but it matches spaces:
^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,}[^[:punct:]]$
Any ideas how to not match strings containing punctuation including spaces?
The regular expression you have got there does the following for as far as I understand (I'm not familiar with the ruby variety, and still quite new to regex myself; this will give you an idea, but may not be 100% correct):
Go to the beginning of the string
Ensure the string matches any number of any characters followed by a lowercase letter, e.g. --a
Ensure the string matches any number of any characters followed by an uppercase letter, e.g.--aA
Ensure the string matches any number of any characters followed by a number, e.g. --aA0
If that is all true, make sure the beginning of the string is followed by at least 6 random characters, e.g.--aA0-
Ensure that is followed by a single non-punctuation character (although this is the part I'm not sure about, as I haven't used character classes before, and don't know if it's [^[:punct:]] or [^:punct:]), e.g. --aA0-c
Ensure that is followed directly by the end of the string
Now, the lookaheads would also allow a different order of occurrences, e.g. 0---Aa, as long as the string contains any characters followed by what they are looking for.
What you probably want is ^[a-zA-Z0-9]{6,}$, i.e. at least six characters, with the characters being letters and numbers (though that would also allow aaaaaa, for example).
Maybe try ^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{6,}$ to make sure each group is present, and to get alpha-numerical characters (at least six of them) only.
I always use a tool such as http://www.regexpal.com/ to slowly build up my regex and to see where I go wrong, deconstructing a "bad" regex until I get to a "good" one, then slowly adding to it again.
Hope that helps. :)
P.S.: I'm still a bit unclear how many characters you want to match in total, i.e. if the string is fixed length or not...?

Regex that allows for A-z, 0-9, and dashing in the middle, never on the ends?

I'm working to create a ruby regex that meets the following conditions:
Supported:
A-Z, a-z, 0-9, dashes in the middle but never starting or ending in a dash.
At least 5, no more than 500 characters
So far I have:
[0-9a-z]{5,500}
Any suggestions on how to update to meet the criteria above?
Thanks
[A-Za-z\d][-A-Za-z\d]{3,498}[A-Za-z\d]
If you are willing to treat _ as a letter also, it's even simpler:
\w[-\w]{3,498}\w
This should work:
[0-9A-Za-z][0-9A-Za-z-]{3,498}[0-9A-Za-z]
Here you go:
/^[0-9A-Za-z][0-9A-Za-z\-]{3,498}[0-9A-Za-z]$/
or if you want the beginning and end to be only 0-9,A-Z,a-z (instead of non dash) then:
Explanation:
The first ^ matches beginning of string.
The next [] matches a A-Z,a-z,0-9
The next [] matches 3 to 498 chars of A-Z,a-z,0-9,dash. Note that we match 3 to 498 chars because we match one char in the beginning and one in the end.
The next [^] is again a A-Z,a-z,0-9.
And lastly we match $ for the end of the string.
This assumes that there are either always dashes or never dashes. It also assumes only one dash is allowed between alphanumeric characters. It's the only way I can think off hand to limit characters instead of number of instances of the string.
(([0-9a-zA-Z]{4,499})|([0-9a-zA-Z][\d]?){2,249})[0-9a-zA-Z]
Assuming there's no limit to the number of adjacent dashes allowed, this would work:
[0-9a-zA-Z][0-9a-zA-Z\d]{3,498}[0-9a-zA-Z]

regex for matching german postal codes but not a

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*$

Regular expression to exclude special characters

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?

Resources