I need a reg-ex for a pattern in a text box where pattern be as follow :
+1245 here first always should be a plus sign (+) and rest of all 4 should be digits (0 to 9)
I have used the other available reg-ex on google but they are mostly for the complete phone number not for extension .
Try this regex:
^[+][0-9]{4}.*$
This will match a leading plus followed by exactly four digits, which can also be followed by anything else.
If your entire string is as described, use this pattern:
^\+\d{4}$
^ means the beginning of the string
\+ means + (escape the + sign)
\d{4} means exactly 4 digits
$ means the end of the string
Related
I am trying to write a regex to handle these cases
contains only alphanumeric with minimum of 2 alpha characters(numbers are optional).
only special character allowed is hyphen.
cannot be all same letter ignoring hyphen.
cannot be all hyphens
cannot be all numeric
My regex: (?=[^A-Za-z]*[A-Za-z]){2}^[\w-]{6,40}$
Above regex works for most of the scenarios except 1) & 3).
Can anyone suggest me to fix this. I am stuck in this.
Regards,
Sajesh
Rule 1 eliminates rule 4 and 5: It can neither contain only hyphens, nor only digits.
/^(?=[a-z\d-]{6,40}$)[\d-]*([a-z]).*?(?!\1)[a-z].*$/i
(?=[a-z\d-]{6,40}$) look ahead for specified characters from 6 to 40
([a-z]).*?(?!\1)[a-z] checks for two letters and at least one different
See this demo at regex101
This pattern with i flag considers A and a as the "same" letter (caseless matching) and will require another alpbhabet. For case sensitive matching here another demo at regex101.
You can use
^(?!\d+$)(?!-+$)(?=(?:[\d-]*[A-Za-z]){2})(?![\d-]*([A-Za-z])(?:[\d-]*\1)+[\d-]*$)[A-Za-z\d-]{6,40}$
See the regex demo. If you use it in C# or PHP, consider replacing ^ with \A and $ with \z to make sure you match the entire string even in case there is a trailing newline.
Details:
^ - start of string
(?!\d+$) - fail the match if the string only consists of digits
(?!-+$) - fail the match if the string only consists of hyphens
(?=(?:[\d-]*[A-Za-z]){2}) - there must be at least two ASCII letters after any zero or more digits or hyphens
(?![\d-]*([A-Za-z])(?:[\d-]*\1)+[\d-]*$) - fail the match if the string contains two or more identical letters (the + after (?:[\d-]*\1) means there can be any one letter)
[A-Za-z\d-]{6,40} - six to forty alphanumeric or hyphen chars
$ - end of string. (\z might be preferable.)
I have the following strings and need to extract the numbers as follows :
1) M123123123AD123 => 123123123
2) M1231231212MN23 => 1231231212
3) G12312312312DD => 12312312312
I am currently reading it using "\d+[0-9]". This works well if there is 1 number after the second set of characters. But if there are multiple numbers after the second character set, the above regex string picks them too. For example, 'M123123123AD123' will give 123123123123. But the last 123 should not be there.
You want to get a streak of digits in between two letters.
You can use
(?<=[a-zA-Z])\d+(?=[a-zA-Z])
See the .NET regex demo.
Or, if you want to get the digits after the leading non-digit chars, use
(?<=^\D+)\d+(?=[a-zA-Z])
See this .NET regex demo.
In C#, you can use Regex.Match:
var result = Regex.Match(text, #"(?<=^\D+)\d+(?=[a-zA-Z])")?.Value;
Regex details:
(?<=[a-zA-Z]) - right before the current location, there must be an ASCII letter (use \p{L} to match any letter)
(?<=^\D+) - right before the current location, there must be start of string + any one or more non-digit chars (use \D* if the digits can appear at the start of string)
\d+ - one or more digits
(?=[a-zA-Z]) - right after the current location, there must be an ASCII letter (use \p{L} to match any letter).
First time posting, please forgive the formatting. Not really a programmer, I work in C# with the Revit and AutoCAD APi's. Important to note, as the Revit API is a bit of mess, so the same code may produce different results in a different API. So I have three basic string patterns where I want to return certain numbers depending on what their prefix & suffix. They could be surrounded by other text than what I show, and the actual numbers and positions within the string may vary.
String 1: (12) #4x2'-0 # 6 EF
String 2: (12) #4 # 6 EF
String 3: STAGGER 2'-0, SPCG AT 6 AT 12 SLAB
The code I'm using:
if (LengthAsString.IsMatch(remarkdata) == true)
{
Regex remarklength = new Regex(#"isnertRegexPatternhere");
if (remarklength.IsMatch(remarkdata))
{
remarkdata = remarklength.Replace(remarkdata, "${0}\u0022");
}
}
remarkdata is the strings from above, and im adding inch marks " after each number match.
The patterns ive tested and their returns:
String 1 String 2 String 3
\d+(?!['-]|([(\d+)])) 0,6 4,6 0,6,12
(?<![#])\d+ 12,2,0,6 12,6 2,9,6,12
\d+(?= #)|(?<=# )\d+ 0,6 6 no matches
expected results: 0,6 6 0,6,12
so im close, but no cigar. Thoughts?
Double Edit: looking for the numbers that aren't preceded by #, nor between (). Ignore # and x, they may or may not be there.
You seem to be looking for
(?<!#)\d+(?!.*(?:['-]|[#x]\d))
See the regex demo
Details
(?<!#) - a negative lookbehind that fails the match if there is a # immediately to the left of the current location
\d+ - 1 or more digits (or [0-9]+ to only match ASCII digits)
(?!.*(?:['-]|[#x]\d)) - a negative lookahead that fails the match once there are any 0+ chars other than newline (.*) followed with ', -, or #/x followed with a digit immediately to the right of the current location.
Note that in case your strings always have balanced non-nested parentheses, and you may have (123) substrings after # or x1, you may also want to add [^()]*\) into the lookahead
(?<!#)\d+(?!.*(?:['-]|[#x]\d)|[^()]*\))
to avoid matching digits inside the parentheses.
See another .NET demo.
I want regex which will allow following format
1234567-8
123456B
Now here if user enter second pattern then he should be lock to enter maximum 7 characters so
1234568B
123456V1
this becomes invalid
I have tried
[0-9]{7}-[0-9]|[[0-9]{6}[A-z]{1}]{7,7}
but this fails
For the sample input you provided, you may use ^([0-9]{7}-[0-9]|[0-9]{6}[A-Za-z])$.
A bit more contracted version: ^[0-9]{6}(?:[0-9]-[0-9]|[A-Za-z])$.
Note that 1234567-8 has 7 digits and a hyphen followed with a digit, so the whole string length cannot be limited to just 7 characters all in all.
In .NET and almost all other regex flavors [A-z] is a mistake, as it can match more than just letters.
Placing a quantifier {1} into a character class makes it a simple symbol combination, so [{1}] matches either { or 1 or }.
The {7,7} (={7}) will not limit the whole string length to 7, as you do not have anchors (^ and $) around the expression and you "ruined" the preceding quantifiers by putting them into a character class.
I think this is what you need:
^(\d{7}-\d|\d{6}[A-Z])$
7 digits, dash, digit OR 6 digits, 1 large latin letter.
^\d{6}(?:\d-\d|[A-Z])$
It can satisfy well with 2 your above formats
1234567-8
123456B
I am trying to create a regex to validate a string. The string could be of the following formats (to give an idea of what I am trying to do here):
145/1/3 or
748/57676/6765/454/345 or
45/234 45/235 45/236
So basically the string can contain numbers, spaces and forward slashes and the string can end with a number only. I am new at regex and have gone through many of the questions on the website. But please you have to admit that this is really confusing and difficult to master. And if someone could refer an author or any weblink that can teach regex, that would be really helpful. Thanks in advance mates!
I came up with this
^[0-9]( |[0-9]|\/)*[0-9]$
And used this to test it.
You can see it matches anything that begins (^) with a number, has zero or more (*) of either a space, a number or (|) a forward-slash (/) and ends ($) with a number.
Now that I am aware that the space and / cannot go together and multiple spaces and/or slashes are also not allowed, this RegEx is a better fit for you.
^[0-9]+([ \/][0-9]+)*$
This should work: ^[/\d\s]*\d$.
It is looking for the beginning of the string ^ , then 0 or more digits, spaces [/\d\s]* followed by a digit \d then the end of the string $.
You should use following regular expression:
(\d+(/\d+)*\s*)+
This mean: some digits (\d+) followed by optional repeating pattern of some digits and \ ((/\d+)*) followed by an optional number of whitespaces (\s*), all repeated at least once.
Try this:
^\d(\d|\s|\/)*\d$
\d = digit character (you can also use [0-9]).
\s = space character
The brackets followed by a star means to repeat a \d, \s, or / an infinite amount of times.
The final \d$ means the ending must match a digit.