Regular Expression for Employee ID with some restrictions [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Problem Statement:
I'm trying to build a regular expression which accepts two consecutive special characters like: /_ or \\ or ./ or -- or \- or any other combination of special charcters (./\_-),in the regular expression mentioned below:
^[a-zA-Z0-9\d]{1}[a-zA-Z0-9\d._/\-]{0,49}$
What i'm doing wrong here?

mlorbetske's regex can be rewritten a bit to remove the use of conditional regex. I also remove the redundant 0-9 from the regex, since it has been covered by \d.
^[a-zA-Z\d](?:[a-zA-Z\d]|(?<![._/\\\-])[._/\\\-]){0,49}$
The portion (?:[a-zA-Z\d]|(?<![._/\\\-])[._/\\\-]) matches alphanumeric character, OR special character ., _, /, \, - if the character preceding it is not a special character already. I also make the group non-capturing (?:pattern), since it seems that the regex is used for validation only.
I made use of the zero-width negative look-behind assertion (?<!pattern) to assert the character in front is not one of the special characters.

This regex seems to match what you're asking for
^[a-zA-Z0-9\d](?(?<=[\._/\\-])[a-zA-Z0-9\d]|[a-zA-Z0-9\d\._/\\\-]){0,49}$
Example
Regex.IsMatch("a-12--3", Pattern); //false
Regex.IsMatch("a-12-3", Pattern); //true
I've used a conditional (?true|false) syntax to indicate that if the preceding character (before entering the middle group) is one of the punctuation characters, only non-punctuation characters may follow it, otherwise any of the specified characters are allowed.
The (?<=expression) syntax is a zero-width positive look-behind.

Related

C# - Regular expression [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I'm studying a program written in c #.
I do not understand this regular expression, can you explain to me what it does?
Thank you
Match numberMatch = Regex.Match(patternOutput, "(#+)([\\.|,])(#+)");
https://regex101.com/ provides a convenient explanation to pretty much all possible kinds of regex syntaxes. In your case the regex matches a literal # character at the beginning, followed by one of the characters in the square brackets (each of them literally, where the backslash is escaped by another one): [\\.|,], so this will match either \, ., |, ,. And at the end follows one more # character.

Regular Expression to check doubles, triples etc [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I need regular expression to retrieve no of doubles, triplets, tetras etc from a telephone number
following is the example,
number is 1001055522
it should return me
group Numbers
=============================
Doubles 00
22
Triplets 555
This regex when used with Regex.Matches will produce exact double or triple (not part of longer consecutive sequence). This is due to the greediness of the quantifier.
(\d)\1+
Demo
Well, the rest is to check the length of the string and count... I will leave it to you.
To find doubles, use a backreference:
(.)\1
Here's a demo: http://regex101.com/r/zC3fM1
To find triplets, just repeat the backreference:
(.)\1{2}
Here's a demo: http://regex101.com/r/cJ4lJ8
If you want to match all consecutive numbers regardless of how many there are, then use + on the backreference:
(.)\1+
Here's a demo: http://regex101.com/r/pL8sB3
Dim n = "1001055522"
Dim doubles = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1")
Dim triples = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1{2}")
'Doubles
For Each d As System.Text.RegularExpressions.Match In doubles
Console.WriteLine(d.Value)
Next
'Triples
For Each t As System.Text.RegularExpressions.Match In triples
Console.WriteLine(t.Value)
Next

Is there a Regular Expression that will never match any string? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Sort of a two part question:
Is there any theoretical regular expression that will never match any string (using general syntax without any fancy stuff provided by modern regular expression matchers)?
Is there a simple way to use C#'s Regex syntax to create a regex that will never match any string (this time, all the fancy stuff is included)?
NOTE: I am not referring to matching the empty string (that would be easy, just "").
Without multi-line mode, the end doesn't usually tend to appear before the beginning:
$.^
Or more simply, again without multi-line mode:
$.
With lookarounds, you can do all kinds of contradictory stuff:
(?=a)(?=b)
This forces a character to be two different things at once, which is of course impossible.
You could use contradictory lookbehinds, for example
\w(?<!\w)
Here \w will match any word character and the lookbehind (?<!\w) will make sure that the last character was not a word.
Just as you can match any characters with [\s\S], you can match no characters with [^\s\S] (or [^\w\W], etc).

Regex match xml attribute value [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to get the integer value of this xml attribute limit=\"25\"
I tried this :Match match = Regex.Match(response.Content, "(?<=limit=))\\d+");
gives me an error : "too many )'s.
and this : Match match = Regex.Match(response.Content, #"limit=([0-9])$"
this returns nothing, the match is not successful
From this xml:
<issues type="array" limit="25" total_count="251" offset="0">
<issue>
<id>4317</id>
Your first regex has too many )s in it. Count them.
Your second is failing because of the quotation marks around the attribute value. Try "limit=\"([0-9])\"$" instead.
Lots of people will tell you to use an XML parser instead. I would strongly recommend that if you're doing anything more than very minor extraction of data from well-known XML, because XML itself isn't parseable with regular expressions.
Regex can be used for parsing XML since it is strict with its format but it is not recommended to use it
Use LINQ2XML
XElement doc=XElement.Parse(response.Content);
var value=doc.Attribute("limit").Value;
OR
var value=Regex.Match(response.Content, #"limit=""(\d+)""",RegexOptions.Singleline).Groups[1].Value;
It's better to use
string TheStringNeededToBeParsed = "... the xml ";
TheStringNeededToBeParsed .IndexOf(...

What is the regex for this? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
First of all I guess I will start by asking what are some good tools or references for building regex strings? I usually find them on the net, but I would love to learn them a little more.
Now on to my original question: what is the Regex to find a full string, or find a line that contains the string. The string is:
** Start of
The regex you are looking for is: \*\* Start of.*
Because C# has its own escape characters you may want to put this in a verbatim string like #"\*\* Start of.*".
The best tool for helping you build, learn and understand regular expressions is RegexBuddy. It helps you see the meaning of your expressions as well as test them through an intuitive graphical UI.
The most complete resource for information on regular expressions (across different languages) is http://www.regular-expressions.info/ . If you are looking to learn about a specific Regular Expression implementation you might be better of reading the implementation-specific documentation/spec. For .NET, a good starting place would be the Regex documentation at MSDN You can also test .NET regular expressions quickly online with the free tool available at http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
I also would like to note that I agree with #ziesemer that using an IndexOf or StartsWith method is probably a better solution for such a simple pattern.
I think you're using the wrong tool for the job. Regular expressions are best suited for finding patterns. It seems you're only looking to do a simple search - use the proper API (e.g. IndexOf) for this.
Otherwise, you simply need to escape the asterisks - which are special characters in regular expressions - meaning "match 0 or more of":
\*\* Start of
While very informative, none of the answers provide the correct regex for your specific problem. Here it is:
string regexPattern = #"^.*?\*{2} Start of.*?$";
Note that you will have to specify multiline option when searching for match.
You can see the results here.
And here's the explanation of the pattern:
^.*?\*{2} Start of.*?$
Options: ^ and $ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “*” literally «\*{2}»
Exactly 2 times «{2}»
Match the characters “ Start of” literally « Start of»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
For learning regex you could check the Regular Expression Basic Syntax Reference on www.regular-expressions.info and also additionally A Gentle Introduction: The Basics
And regarding the string to find if you want only character from a to z then I think you should write as
^[a-zA-Z]$
This will take small and capital a to z characters.
Update
^\*\* Start of(.*?)$
Spliting Detail
\*, take asterisk into consideration
Start of, compare exactly the this string
(.*?), take anything on that single line
^\*\* Start of(.*?)(([\n]*(.*?)){19})*$
Spliting Detail
\*, take asterisk into consideration
Start of, compare exactly the this string
(.*?)(([\n]*(.*?)){19})*, take anything but limit upto 19 lines

Categories