Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need a regex for a preg_match function that matches the following:
a2354 - r2343
Where the 5-character strings can be the following:
1st character: any uppercase/lowercase character from the English alphabet.
2nd-5th character: any numerical digit from 0-9.
The first and second group of 5 characters must have a space before and after although the space should include any weird charset characters that present as a space.
There must be a string between them that matches ' - ' (space, dash, space) although the spaces should include any weird characters that could present as a space and the dash should be any character that presents as a dash.
From your description, I think you're looking for:
/\s[a-z][0-9]{4}\s-\s[a-z][0-9]{4}\s/i
However, it's a little bit unclear. I'll break it down, and then include links to the documentation, so you can fill in the ambiguous parts on your own.
1) \s: anything considered a "white-space" character
2) [a-z]: the normal English alphabet (lower/uppercase handled by 'i' flag at the end
3) [0-9]: the normal Euro-centric numerals
4) {4}: repeats exactly 4 times
1 is called an escape sequence; 2 and 3 are documented at Character classes; 4 is repetition. Lastly, the trailing i is called a Pattern modifier.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to do Regex code for product's code in VSCode's HTML. My product's code has the following conditions:
Required enter 6 characters
First 2 characters must be letter and uppercase
Next 4 characters must be numbers.
I have tried this regular expression and it doesn't work:
^[A-Z]{2}+\[0-9]{4}$
Your regex should be:
^[A-Z]{2}[0-9]{4}$
This corrects the escaping of your character class; that made it no longer a character class but a series of characters to match in the regex, ending with 4 ]s. The + also is not needed as the {2} is stating only 2 uppercase alpha characters are allowed.
You can also swap the [0-9] with \d which is the metacharacter for an integer. With PHP regexs you also need delimiters so something like:
/^[A-Z]{2}\d{4}$/
could be used in preg_match.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to submit some information to an API but one of the error messages I get back is:
Field must contain only: upper case letters (A-Z), numbers (0-9), full
stop (.), forward slash (/), dash (-), Ampersand (&) and space
Using regular expression in PHP, can a rule be written that replaces any characters other than the ones specified in the error message?
I suck at regular expression but I bet this is quite easy for someone else! Thank you.
I can obviously set the whole thing to strtoupper() so the first bit I can take care of!
The easiest way would be to use preg_replace.
Example: $str = preg_replace('/[^A-Z\d.\/\-&]+/', '', $str);
Explanation:
[^A-Z\d./-&]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
A-Z a single character in the range between A and Z (case sensitive)
\d match a digit [0-9]
. the literal character .
/ matches the character / literally
- matches the character - literally
& a single character in the list & literally (case sensitive)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here I have regex, in which I want it to filter only word length >8 and <15 characters. I added
(?=.{8,14})b$
REGEX-http://regex101.com/r/sA5xL5
but it does not make any diff.
`^(?=.{8,14})b$\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$^|^2(?:0[01378]|3[0189]|4[017]|8[0-46-9]|9[012])\d{7}|1(?:(?:1(?:3[0-48]|[46][0-4]|5[012789]|7[0-49]|8[01349])|21[0-7]|31[0-8]|[459]1\d|61[0-46-9]))\d{6}|1(?:2(?:0[024-9]|2[3-9]|3[3-79]|4[1-689]|[58][02-9]|6[0-4789]|7[013-9]|9\d)|3(?:0\d|[25][02-9]|3[02-579]|[468][0-46-9]|7[1235679]|9[24578])|4(?:0[03-9]|2[02-5789]|[37]\d|4[02-69]|5[0-8]|[69][0-79]|8[0-5789])|5(?:0[1235-9]|2[024-9]|3[0145689]|4[02-9]|5[03-9]|6\d|7[0-35-9]|8[0-468]|9[0-5789])|6(?:0[034689]|2[0-689]|[38][013-9]|4[1-467]|5[0-69]|6[13-9]|7[0-8]|9[0124578])|7(?:0[0246-9]|2\d|3[023678]|4[03-9]|5[0-46-9]|6[013-9]|7[0-35-9]|8[024-9]|9[02-9])|8(?:0[35-9]|2[1-5789]|3[02-578]|4[0-578]|5[124-9]|6[2-69]|7\d|8[02-9]|9[02569])|9(?:0[02-589]|2[02-689]|3[1-5789]|4[2-9]|5[0-579]|6[234789]|7[0124578]|8\d|9[2-57]))\d{6}|1(?:2(?:0(?:46[1-4]|87[2-9])|545[1-79]|76(?:2\d|3[1-8]|6[1-6])|9(?:7(?:2[0-4]|3[2-5])|8(?:2[2-8]|7[0-4789]|8[345])))|3(?:638[2-5]|647[23]|8(?:47[04-9]|64[015789]))|4(?:044[1-7]|20(?:2[23]|8\d)|6(?:0(?:30|5[2-57]|6[1-8]|7[2-8])|140)|8(?:052|87[123]))|5(?:24(?:3[2-79]|6\d)|276\d|6(?:26[06-9]|686))|6(?:06(?:4\d|7[4-79])|295[567]|35[34]\d|47(?:24|61)|59(?:5[08]|6[67]|74)|955[0-4])|7(?:26(?:6[13-9]|7[0-7])|442\d|50(?:2[0-3]|[3-68]2|76))|8(?:27[56]\d|37(?:5[2-5]|8[239])|84(?:3[2-58]))|9(?:0(?:0(?:6[1-8]|85)|52\d)|3583|4(?:66[1-8]|9(?:2[01]|81))|63(?:23|3[1-4])|9561))\d{3}|176888[234678]\d{2}|16977[23]\d{3}|7(?:[1-4]\d\d|5(?:0[0-8]|[13-9]\d|2[0-35-9])|624|7(?:0[1-9]|[1-7]\d|8[02-9]|9[0-689])|8(?:[014-9]\d|[23][0-8])|9(?:[04-9]\d|1[02-9]|2[0-35-9]|3[0-689]))\d{6}|76(?:0[012]|2[356]|4[0134]|5[49]|6[0-369]|77|81|9[39])\d{6}|80(?:0\d{6,7}|8\d{7})|500\d{6}|(?:87[123]|9(?:[01]\d|8[0-3]))\d{7}|8(?:4[2-5]|70)\d{7}|70\d{8}|56\d{8}|(?:3[0347]|55)\d{8}|8(?:001111|45464\d)$|(?:\((\+?\d+)?\)|(\+\d{0,3}))? ?\d{2,3}([-\.]?\d{2,3} ?){3,4}`
please dont bother about regex length.
It matches even if match length exceeds
What is missing to restrain it to filer 8-14 length pattern match only.
First off, ^(?=.{8,14})b$ means "at the beginning of the string as asserted by ^, look ahead to see if we can find a single character between 8 and 14 times, and if yes, then match a single character b then the end of the line $. You cannot have one line that is both a single character b and 8 characters in length. This part of the expression can never match. See demo.
But your regex still finds an overall match. Why? Clearly, even if ^(?=.{8,14})b$ were able to match anything, it does not set a condition for the whole expression, because something later in the regex overrides it: an alternation (|) which means that we can match what's on the left OR what's on the right.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi so I need to write a program where it validates input.
The specifications are " letters, spaces, colon, semi-colon, dash, comma, apostrophe and numeric character (0-9) only - cannot be blank"
I know how to do this when I have an explicit number of characters to use, but in this case it can be any amount of characters, 1-50 and I do not know what to do for regular expression notation in this case of any number of characters. How can I start this?
When you say that the input line cannot be blank I imagine that, while you want to allow spaces, the line itself cannot be spaces only.
Based on that perception I suggest this regex. The negative lookahead, in parentheses, make sure that the line is not all spaces.
^(?!\s+$)[-A-Za-z0-9:;'\s]{1,50}$
You just need the + character if you're trying to match any number of something (but at least one). Here's is an example:
^[A-Za-z :;\-,'\d]+$
To match the whole string:
^[A-Za-z0-9 :;,'-]{1,50}$
To match it as part of a string:
[A-Za-z0-9 :;,'-]{1,50}
There are several options to represent multiplicities in regular expressions:
k? means there is either 0 or 1 k possible, i.e. or k
k*means there are 0 or more ks possible, e.g. , k, kk, ...
k+ means there are 1 or more ks possible, e.g. k, kk, ...
k{5} means there are exactly 5 ks, i.e kkkkk
k{3,} means there are at least 3 ks, e.g. kkk, kkkk, ...
k{2,4} means there are either 2, 3 or 4 ks.
For further reading please see the corresponding page of the PHP Documentation.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Basically I need to match something like this:
0000-000 Text with spaces
Where 0000-000 and 0 is any number, followed by a space followed by arbitrary text, with spaces.
I have the numbers down:
/^\d{4}(-\d{3})?$/
but I'm having a hard time getting the text...
It's close, but you would use this pattern to match the text as well:
/^\d{4}(-\d{3})? ([\w\s]+)$/
From the documentation:
\d any decimal digit
\s any whitespace character
\w any "word" character
A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". The definition of letters and digits is controlled by PCRE's character tables, and may vary if locale-specific matching is taking place. For example, in the "fr" (French) locale, some character codes greater than 128 are used for accented letters, and these are matched by \w.
Try this regex
/^\d{4}(-\d{3})? .+$/
For people who DON'T assume everyone just uses the standard U.S. English charset:
/^\d{4}(-\d{3})? ([\p{L}\s]+)$/u
\p{L} matches any Unicode codepoint that is classified as a letter, regardless of language. The u flag is required at the end so that PHP's PCRE engine expects Unicode.
If you want to match only text and spaces after the numbers, you can do:
/^\d{4}(-\d{3})?[ a-zA-Z]+$/
Here's an interactive regex editor (made for Ruby but works for php)
http://rubular.com/r/ocbo5Sea8m
[0-9]{4}-[0-9]{3} .+
Seems to work