This question already has an answer here:
Regex to match any combination and number of whitespaces and linebreaks between groups
(1 answer)
Closed 2 months ago.
I do have search pattern for Regex, but my multiple file have different number of spaces between them. So I need to ignore them in my pattern.
const string PATTERN = #"OTPM = true";
Can someone modify this line for me? I tried different solutions which I found here, but didnt work, since I am a bit new to C#
OTPM\s+= true
Worked perfectly fine. Thank you very much, for all responses
Related
This question already has answers here:
Regular expression for version numbers
(8 answers)
Closed 2 years ago.
I have a required to validate the version numbers using regex. I tried few but not working as expected.
Any idea what regex to use to validate versions like 1.0.10.135 in C#. Every number should grow upto 3 digits in each section.
Update I tried this "[\d]{1,3}.[\d]{1,3}.[\d]{1,3}.[\d]{1,3}" and it accepted "1.0.0.3##", which is not correct.
If you want to go with a regex, I'm surprised you haven't found one that worked. Here's a quick one (it worked the first time I tried it):
(?<major>\d{1,3})\.(?<minor>\d{1,3})\.(?<patch>\d{1,3})(\.(?<build>\d{1,3}))?
It names the parts, and makes the last part optional
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 an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I'm looking for a suitable regex for hyperlinks.
I found a link to this website: https://mathiasbynens.be/demo/url-regex, which has several choices. (There are too many to list here.)
However, these do not seem to be compatible with .NET's Regex syntax. I tried using the "cowboy" pattern vs several hyperlinks, e.g. http://www.cnn.com, but no match results.
What syntax are these, and how do I get these to work with .NET? If these require manual tweaking, then just showing how to get the "cowboy" pattern to run will suffice.
You need to only copy the pattern in between the tildes.
static void Main(string[] args)
{
string pattern = "(?:\\b[a-z\\d.-]+://[^<>\\s]+|\\b(?:(?:(?:[^\\s!##$%^&*()_=+[\\]{}\\|;:'\\\",.<>/?]+)\\.)+(?:ac|ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|asia|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|cat|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|coop|com|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|info|int|in|io|iq|ir|is|it|je|jm|jobs|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mobi|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|travel|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn--0zwm56d|xn--11b5bs3a9aj6g|xn--80akhbyknj4f|xn--9t4b11yi5a|xn--deba0ad|xn--g6w251d|xn--hgbk6aj7f53bba|xn--hlcj6aya9esc7a|xn--jxalpdlp|xn--kgbechtv|xn--zckzah|ye|yt|yu|za|zm|zw)|(?:(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])\\.){3}(?:[0-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5]))(?:[;/][^#?<>\\s]*)?(?:\\?[^#<>\\s]*)?(?:#[^<>\\s]*)?(?!\\w))";
string url = "http://www.stackoverflow.com";
Console.WriteLine(Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase));
}
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}$
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to split a string while preserving line endings?
How do I split a string by strings and include the delimiters using .NET?
I'm splitting text into sentences. mystring.Split('.','!', '?') returns the sentences without the ./!/? on them. I need to have it return a sentence with the split param on the end? How does that go? Thanks
public static string[] GetSentences(string text)
{
return text.Split('.', '!', '?');
}
I can think of one way to do it, by combining two separate arrays, but I think it looks awful so I thought I'd ask you professionals for a "proper" way :D
Edit - never mind close its a duplicate. I found the other threads, sorry
Right, string.Split() isn't the right tool here.
Either simply loop through it (string.IndexOf())
or use a RegEx: ([^\.!?]+[\.!?])*
I'm not 100% sure about the escaping.