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
Related
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 6 years ago.
Issue
I am having an issue creating a regex to accept any string and the ENTER key, at the moment i have this:
^$|^.+$
I have looked around and people have said to add \n but this does not work.
An example of the string is should allow is as follows:
Hello this is a test string
and i want this to be accepted
Try setting the s flag on the regex engine. This will ensure that the . metacharacter will match newlines.
Here's a link to a working example.
Also, as a sidenote, instead of ^$|^.+$ you can condense the whole expression to ^.*$ to achieve the same results with better performance.
In C#, you need the RegexOptions.Singleline option. See this SO post for more information.
Here is a quick example that really just matches the entire string, so it's not useful.
var regex = new Regex(#"^.*$",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
In your future validation code, you need to replace .* with whatever your validation will be.
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 9 years ago.
My question was marked as a duplicate so I've made a couple edits. As I said, I was able to find many similar questions when I searched but none were quite what I needed. I am not validating a string where the only thing present will be the phone number (this seems to be what most of the other questions are addressing). Rather, I am attempting to pull out all phone numbers (which will then be manually checked by the user) from a larger block of text. The problem I am having is that my regular expression is matching zip codes with extensions (ex: 45202-4787), and I am not sure how to alter my regex to avoid that. If this truly is a duplicate question then I apologize for not being able to find the existing one that deals with my issue.
My specifications for phone number format are:
1) -, ., and space as delimiters (and in any combination)
2) area code may appear with or without parentheses
A few examples:
(xxx) xxx-xxxx
(xxx) xxx.xxxx
xxx-xxx-xxxx
xxx xxx-xxxx
xxxxxxxxxxx
I am using Anirudh's regex from the comments:
(\(?\d{3}\)?)?[. -]?\d{3}[. -]?\d{4}
Again, my problem is that this regex matches zip codes with extensions (ex: 45202-4787).
I would be grateful for any help, as I'm very new to using regular expressions. Thanks!
This should do it:
^(\([0-9]{3}\)|[0-9]{3})[ -\.]?[0-9]{3}[ -\.]?[0-9]{4}$
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).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression for validating numbers with one space and one optional special character
I have to write a regular expression to validate mobile numbers which look like below:
a).Maximum 12 characters
b).Will allow a space (at a non-defined point)
c).Must start with 0
d).Followed by an optional extension number of up to five digits in length, not including the # sign
I have tried with the following to get 0-12 numbers. but i don't know how to add the optional extension numbers part (the # and followed by numbers)
^(0(?:\d{0,11}|(?=\d* \d*$)[\d ]{0,12}))$
You need to create regular expressions for each format. You can then combine them into a single regular expression using the alternation construct |.
The first pattern is the most tricky one but you have provided a way to solve it by using a zero-width positive lookahead assertion yourself (here I assume that the space is only allowed between numbers):
(?=\d+ \d+#)[\d ]{12}#\d{5}
The next pattern is straightforward:
\d{11}
You definition of the last pattern is a bit vague but doing various assumptions I have selected this pattern:
\d{1,12}#\d{1,5}
The combined regular expression is then:
^(?=\d+ \d+#)[\d ]{12}#\d{5}|\d{11}|\d{1,12}#\d{1,5}$
Try this regex. It will satisfy a,b,c and d conditions
(?<=^)([\d\s]{13}|\d{12}|0\d{11}|\d{5})(?=$)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
i'm doing the simplest regex.match ever, i am giving the Regex.Match a pattern of one character and it returns no match at all, and i made sure the input text contains a lot of that character?
i checked all the usings.
its just very weird.
any help would be appreciated!
Thanks.
EDIT:
my sample is "doing any type of matching is simply not WORKING"
returns an empty match
Match m=Regex.Match(#"c","abcdc");
the code is compiled with no errors, so why the NO MATCHING!!
EDIT: based on your edit the issue is that you're using the parameters out of order. You need to switch the order and supply the input (string source to find a match in) then the pattern (what to match against).
In fact, this order is specified for you by the IntelliSense as depicted in this image:
It usually helps to match the naming suggested by the IntelliSense or refer to it to ensure the proper items are being passed in.
What is the character being used? Chances are you're trying to use a character that is actually a metacharacter which holds special meaning in regex.
For example:
string result = Regex.Match("$500.00", "$").Value;
The above wouldn't return anything since $ is a metacharacter that needs to be escaped:
string result1 = Regex.Match("$500.00", #"\$").Value; // or
string result2 = Regex.Match("$500.00", "\\$").Value; // or
string result3 = Regex.Match("$500.00", Regex.Escape("$")).Value;
For a list of common metacharacters that need to be escaped look at the Regex.Escape documentation.
You have the parameters in the wrong order in your example:
Match m=Regex.Match(#"c","abcdc");
This code means that you try to find the string "abcdc" in the string "c", try it the other way around and it should work better, ie:
Match m=Regex.Match("abcdc", "c");
Also, the fact that your code compiles doesn't mean that it will necessarily find a match...
Here is the documentation for Regex.Match.
I assure you, the regular expression works. I have used it many, many times.
This will put the string "d" in the variable s:
string s = System.Text.RegularExpressions.Regex.Match("asdf", "d").Value;
If that doesn't work, perhaps you have some strange culture setting that affects how strings are compared? Does System.Globalization.CultureInfo.CurrentCulture.DisplayName return an expected value?