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
Related
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 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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i have sequence of strings in excel file.
example:
1780CAR405108CCC72
1780CAR405108KK89.0
1780CAR405108B7888
I need to get the numbers/floats whatever after the last occurrence of an ALPHABET
like in these examples above, after CCC or KK or B. need assistance asap....
preg_match('#[^a-zA-Z]+$#', $string, $result);
[a-zA-Z]([\d\.]+?)$
lazy match anchored at the end of the string, matches digits and fullstops into a capture group.
Also, since you're using PHP, you can use this if you don't want to deal with capture groups:
(?<=[^\d])[\d\.]+?$
You can match
[^a-zA-Z]+$
[^a-zA-Z] is a negated character class, it stands for "one character, any character but the ones inside the class". $ matches the end of the string.
See demo here.
Using preg_match:
preg_match('/[^a-zA-Z]+$/', "1780CAR405108B72", $match);
print($match);
$matches will contain numbers/float after alphabets.
preg_match('/[^a-zA-Z]+$/', "1780CAR405108B72",$matches);
'/pattern/'==> where slash is the delimiter.
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 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
I am new to regular expressions and I need a regex for php username that matches
no spaces
no special characters
in length 8, max 32
and also a regex for password that matches
alphanumeric have at least one digit and one character
no spaces
Try the following REGEX :
^\S+\w{8,32}\S{1,}
^ means beginning of line
\S means all characters but not a space or a tab
+ mean at least one character
\w means an alphanumeric character including _
{8,32} means at least 8 characters, and max 32
\S still means all characters but not a space or a tab
{1,} means at least 1 item