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.
Related
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
To search for a specific string, which is like UE-42F5070, in a text with preg_match function I am trying to write a regular expression. I am not able to write the regular expression pattern for "UE-##F####", where #'s are numbers, to use with preg_match to find them in the text.
Example of the string;
UE-32F5070
UE-50F8000
You can use this regex:
'/^UE-\d{2}F\d{4}$/'
Recommended Reading on Regex
Details:
^ - Start input
UE - Match literal "UE"
\d{2} - Match 2 digits
F - Match literal "F"
\d{4} - Match 4 digits
$ - End input
A number in regex is [0-9] or \d. Repetitions are {n}. So
UE-[0-9]{2}F[0-9]{4}
Or
UE-\d\dF\d\d\d\d
It looks like UE is a constant, so you can start your regex with that as a literal, the rest should just be:
preg_match('/UE-([0-9]{2}F[0-9]{4})/', $string, $matches);
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
Working with a ficticuous string such as;
$string = 'Ford : LTD 1988 Ford Station Wagon with HP 351 H Engine and Performance Transmission';
How could I use rexexp or preg_match (I don't know which would be better either) to extract a sequence of letters and numbers ("HP 351 H") from that string to use in another variable (ie: $EngineSize)
The above is a fictious example, I'm just trying to make it clear that I'm trying to extract letters and numbers from a UI.
NOTE: Being that this is coming from a UI, the engine size may be positioned anywhere in the string and the format may be with or without spaces and may or may not have a letter at the end, as well as the model could be 2 or 3 letters (ie; LE, LT, LTD etc) as well as the engine size could be 2 - 3 digits possibly followed by 2 or three letters).
If anyone wouldn't mind showing me how to write an expression to retrieve this data and explain to me which is better (regexp or preg_match) I'd be most appreciative and I thank you in advance.
The following regex matches exactly what you describe, but there is a good chance of false positives:
/(?<=\s|^)[a-zA-Z]{2,3} ?\d\d\d? ?[a-zA-Z]{1,3}?(?=\s|$)/
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
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