Regular expression (RegEx) not working - regex

I need a regular expression that matches a String at the beginning of the input which satisfies following conditions:
start with a letter
end with a letter or a number
may contain letters, numbers and spaces
I have this expression so far:
^([a-zA-Z]+[a-zA-Z0-9 ]*[a-zA-Z0-9]+)|[a-zA-Z]
http://userguide.icu-project.org/strings/regexp
The OR statement in the expression is to allow a String that consists of one letter.
The problem is that the second part of the OR statement is always preferred, so when the input is query1, it matches only q.
How can I solve this problem?
Is there a way to simplify the expression? My way seems a little to complex for this relatively simple case.

^([a-zA-Z]+[a-zA-Z0-9 ]*[a-zA-Z0-9]+)$|^[a-zA-Z]$
You can make use of ^$ anchors to imply that that it is only for single letter string

You can use this regex to satisfy all conditions:
^[a-zA-Z](?:[a-zA-Z0-9 ]*[a-zA-Z0-9])?$
^[a-zA-Z] matches a letter at start.
(?:...)? is optional part to allow single char input.
[a-zA-Z0-9] in the makes sure last char is alpha-numeric.
RegEx Demo

Regex to match with a character at the start, character, number or spaces in between and ends with character or number:
^[a-z|A-Z][a-z|A-Z|0-9| ]*[a-z|A-Z|0-9]$

Related

Regex pattern for first name without whitespace or numbers

I have this regex pattern
/^[^-\s][^0-9][a-zA-Z\s-]+$/
I am a bit confused on why when I test it on https://www.regextester.com/
My pattern allows one single number to be added before the string. Meaning that if I type in '2Mantas' it will still accept it whereas '22Mantas' will fail the test. I do not want any numbers or whitespace to be allowed. Any ideas anyone?
You have two negation groups so it is saying the first character cannot be whitespace and the second character cannot be a number. If you put the whitespace and digit in the first brackets it will work as desired.
^[^-\s\d][a-zA-Z\s-]+$
The first two rules in your current regular expression break down to the following:
^[^\s-] - the first character in the string should not be a whitespace or a hyphen. This explains why 2steve is accepted - 2 is not a whitespace or a hyphen character.
[^0-9] - the second character in the strnig should not be a digit. This iexplains why 22steve is not accepted - the 2 in the second position is a digit, which violates this rule.
Assuming you don't want anything but capital and lowercase letters in your first name input, and the name shouldn't start with a whitespace or hyphen character, you can simplify to a subset of your current regular expression:
/^[A-Za-z][A-Za-z-\s]+$/
Regex101
This should work
Get string, that starts with exactly one digit, and after this digits should be expression, that contains only strings (greedy)
^\d{1}([a-zA-Z]+)
https://regex101.com/r/wtBwd7/1

Weird in a regular expression

I tried the following regular expression:
Pattern: ((.[^[0-9])+)(([0-9]{1,3}([.][0-9]{3})+)|([0-9]+))
My goal is to match any string (excluding digit number) followed by a specified number, e.g. MG2999, dasdassa33232
I used the above regular expression.
It's weird as follows:
V375 (not matched)
Vv375 (matched)
Vvv375 (not matched, but first character is not matched)
Vvvv375 (matched)
...
I don't understand why the first character is never matched. May I need your help?
For your quick test, please try: http://regex101.com/
Thanks in advance!
--
Vu
(.[^[0-9])+) matches any character (.), followed by any character except digits and [, repeatedly.
You probably want [^0-9]+ here – or, simpler, \D+.
The rest of there regular expression has similar problems but since I don’t know the number format you want to match I cannot correct that.

Condition for max character limit and on minimum character putting condition

I am trying to do do following match using regex.
The input characters should be capital letters starting from 2-10 characters.
If it's 2 characters then allow only those 2 characters which does not contain A,E,I,O,U either at first place or second place.
I tried:
[B-DF-HJ-NP-TV-XZ]{2,10}
It works well, but I am not too sure if this is the right and most efficient way to do regex here.
All credit to Jerry, for his answer:
^(?:(?![AEIOU])[A-Z]{2}|[A-Z]{3,10})$
Explanation:
^ = "start of string", and $ = "end of string". This is useful for preventing false matches (e.g. a 10-character match from an 11 character input, or "MR" matching in "AMRXYZ").
(?![AEIOU]) is a negative look-ahead for the characters A,E,I,O and U - i.e. the regex will not match if the text contains a vowel. This is only applied to the first half of the conditional "OR" (|) regex, so vowels are still allowed in longer matches.
The rest is fairly obvious, based on what you've already demonstrated an understanding about regex in your question above.

Get text using Regular Expression

I have the sentence as below:
First learning of regular expression.
And I want to extract only First learning and expression by means of regular expressions.
Where would I start/
Regular expressions are for pattern matching, which means we'd need to know a pattern that is to be matched.
If you literally just want those strings, you'd just use First learning and expression as your patterns.
As #orique says, this is kind of pointless; you don't need RegEx for that. If you want something more complicated, you'd need to explain what you're trying to match.
Regex is not usually used to match literal text like what you're doing, but instead is used to match patterns of text. If you insist on using regex, you'll have to match the trivial expression
(First learning|expression)
As already pointed out, it is unusual to match a literal string like you are asking, but more common to match patterns such as several word characters followed by a space character etc...
Here is a pattern to match several word characters (which are a-z, A-Z, 0-9 and _) followed by a space, followed by several more word characters etc... It ends up capturing three groups. The first group will match the first two words, the second part the next to words, and the last part, the fifth word and the preceding space.
$words = "First learning of regular expression.";
preg_match(/(\w+\s\w+)\s(\w+\s\w+)(\s\w+)/, $words, $matches);
$result = matches[1]+matches[3];
I hope this matches your requirement.

Regex help with matching

Hello I need coming up with a valid regular expression It could be any identifier name that starts with a letter or underscore but may contain any number of letters, underscores, and/or digits (all letters may be upper or lower case).
For example, your regular expression should match the following text strings: “_”, “x2”, and “This_is_valid” It should not match these text strings: “2days”, or “invalid_variable%”.
So far this is what I have came up with but I don't think it is right
/^[_\w][^\W]+/
The following will work:
/^[_a-zA-Z]\w*$/
Starts with (^) a letter (upper or lowercase) or underscore ([_a-zA-Z]), followed by any amount of letter, digit, or underscore (\w) to the end ($)
Read more about Regular Expressions in Perl
Maybe the below regex:
^[a-zA-Z_]\w*$
If the identify is at the start of a string, then it's easy
/^(_|[a-zA-Z]).*/
If it's embedded in a longer string, I guess it's not much worse, assuming it's the start of a word...
/\s(_|[a-zA-Z]).*/