regex expression to accept ENTER key in string [duplicate] - c#

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.

Related

C# ignoring spaces in search Pattern [duplicate]

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

I want to do a search in real time of objects with Regex in c# [duplicate]

This question already has answers here:
Can I use variables in pattern in Regex (C#)
(2 answers)
Closed 3 years ago.
Is it possible to place a string variable inside a Regex? If so.. how?
I've been playing with regex for 4 hours now and i need just one more thing to finish.
return (new Regex(#"\bA=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
This line basically gets any fractional number like 42/13 only if it's after "A=" from a string and extracts it.
So here's my question - Is it possible to do something like that:
string variable;
Regex(#"\b"variable"=(\d+[/]\d+)").Match(From).Groups[1].Value.Trim()).ToString();
The idea is to make it so whatever is in variable becomes the regex and for example if in the variable we input D it's now D= now A=.
Thanks in advance.
This is string interpolation. You use the $ operator on your strings to use it. Example
string variable = "hello";
Regex regex = new Regex($#"\b{Regex.Escape(variable)}=(\d+[/]\d+)");
You need to concatenate your strings as usual (+) and prepend # to each string if using backslashes without escaping them. You also don't need to encase / in the character class as [/]. Alternatively, as mentioned by Josh in his answer and Ron Beyer in his comment below your question, you can use interpolation.
#"\b" + variable + #"=(\d+/\d+)"
Additionally, you should use the method Regex.Escape() against your variable to ensure any special characters are escaped (this will prevent your pattern from failing or making incorrect matches) - sanitizing your variable.

Not terminated set of [] in regex (C#) [duplicate]

This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 5 years ago.
I'm trying to parse a text looking for data inside this pattern:
{{([^]+)}}
i.e. any sequence of characters between {{ and }} .
But, when I try to build a Regex object:
Regex _regex = new Regex("{{([^]+)}}", RegexOptions.Compiled);
I got this error:
analysis of "{{([^]+)}}" - Set of [] not terminated....
whatever it means...
Someone has an hint?
The purpose of [^...] is to negate character classes present in the specified list. After the ^ symbol, in order to define a correct regular expression, you should include a set of characters to exclude like, for example [^a]+ (this matches one or more characters that don't include the literal a).
The regex you are attempting to define is probably:
{{\s*([\w]+)\s*}}
Visit this link for trying a working demo.
This is because [^] is not a valid regex, because you need to specify at least one symbol that you wish to exclude.
In order to capture the string up to the closing }} change the expression to this:
{{((?:[^}]|}[^}])*)}}
Demo.

Finding character which is not in double quote using Regular Expression [duplicate]

This question already has answers here:
Regex to pick characters outside of pair of quotes
(6 answers)
Closed 7 years ago.
I am new in writing regular expression and I have the following Scenario.
I have a string, like :
string line = "if (true){var data = string.Format(\"something {0} {1}.\", \"is\", \"wrong\");}";
now I need to write a regular expression that just pick the closing curly braces which are not in the double quote
so far I tried this:
"(^(\"[^\"]*\")(}))+"
^(\"[^\"]*\") : I want to Ignore any substring which is inside double quote, AND
(}) : I want to take }
+: for at least 1 occurrence.
But it seems I Did something wrong. Could any one please guide me to sort out where I did the wrong?
Thank you.
You just need these parts of your regex:
(?:\"[^\"]*\")|(})
Regex live here.

C#: RegEx for Url validation [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
I need a regEx to match general URLs
(3 answers)
Closed 9 years ago.
I need a Regex validating Url:
This should be valid
http://www.google.com
https://www.google.com
but not this:
google.com
www.google.com
I know this can be done with Uri.IsWellFormedUriString for example but I need a regex. Found a couple of similar topics but does not fit my case.Thanks!
Are you sure you want regex and not this - Uri.TryCreate?
Also have you gone through this post - What is the best regular expression to check if a string is a valid URL?
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?
try this
Regex urlchk = new Regex(#"((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,15})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?", RegexOptions.Singleline | RegexOptions.IgnoreCase);

Categories