This question already has answers here:
How do I make part of a regex match optional?
(2 answers)
Order of regular expression operator (..|.. ... ..|..)
(1 answer)
Closed 1 year ago.
I am trying to match a substring of "Number" or "Number(s)" in a string using a single Regex. However, I can get them to match individually, but not together.
Individually,
'Number' can match the word number
'Number[(]s[)]' can match Number(s).
However, if I put them together and do "Number|Number[(]s[)]" it is not matching for (s) of "Number(s)".
What I have tried:
1: Put \b boundary around the second string, doesn't work.
2: Use \ to escape, but C# yells at me for unrecognized escape sequence, so I opted out of this option
I know that I can use two regex to do what I want, but I wanted to understand what is wrong here and learn.
Number|Number[(]s[)] wont match Number(s) because it's first part "Number" matches it.
Try change the pattern part order: Number[(]s[)]|Number. This will try to match first with the string with parentheses and if it can't it will try the short form.
Also the pattern should be: Number\(s\)|Number
The unrecognized escape error message comes because if you want this pattern written as a string literal you must escape the backslash signs: "Number\\(s\\)|Number".
Related
This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Match string only if not preceded with another
(1 answer)
Closed 3 years ago.
Reading through stack overflow examples i couldn't find a working solution for the below test case.
I need to match a pattern being tested against a list of strings..
the pattern should match if word1 exist, but word2 doesn't exist before it.
Any character can exist in between.
Examples:
pattern - match if word tty_osc exist and mov_osc doesn't exist anywhere before it.
abd.defg.mov_osc.ccr.tty_osc.val - doesn't match... tty_osc exist but mov_osc also exist before tty_osc
abd.defg.ccr.tty_osc.val - match - tty_osc exist, no mov_osc before
I've tried the following negative lookbehind regex - (?<!mov_osc).*tty_osc
You could use a negative lookahead to assert what is on the right is not your forbidden word followed by the accepted word.
^(?!.*?\bmov_osc\b.*?\btty_osc\b).*?\btty_osc\b.*$
Regex demo
You need to move the .* inside of the negative look behind (?<!mov_osc.*)tty_osc. Otherwise it will match everything before tty_osc and the negative look behind would just check against the beginning of the string.
RegexStorm.Net Demo
This question already has answers here:
C# Code to generate strings that match a regex [closed]
(4 answers)
Closed 3 years ago.
Based off a regex string I would like to get a list of all the possible strings that would match the regex.
Example:
Given a regex string like...
^(en/|)resources/case(-| )studies/
I want to get a list of all the possible strings that would match the regex expression. Like...
^en/resources/case-studies/
or
^/resources/case-studies/
or
^en/resources/case studies/
or
^/resources/case studies/
Thank you
Note that in regex ^ denotes the beginning of the line. You must escape it
Try
\^(en)?/resources/case(-|\s)studies/
explanation:
\^ is ^ escaped.
(en)? is optionally en, where ? means zero or one times.
/resources/case the text as is.
(-|\s) minus sign or white space.
studies/ the text as is.
See: https://dotnetfiddle.net/PO4wKV
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.
This question already has answers here:
C# Regex to match the word with dot
(3 answers)
Regex plus vs star difference? [duplicate]
(9 answers)
Closed 3 years ago.
I have some validation code that checks a string against a regular expression.
Regex regex = new Regex(RegexPattern);
if (!regex.IsMatch(value))
{
errorMessage = "The data is not in the correct format.";
return false;
}
If I set the regular expression pattern to ^[0-9]*.[0-9]*.[0-9]*.[0-9]*$, it correctly accepts 1.0.0.0; however, it also accepts 1.0.0..
How can I modify the pattern so that 1.0.0.0 is accepted but 1.0.0. is rejected?
[0-9]* means 0 or more occurrence of [0-9]
[0-9]+ means 1 or more occurrence of [0-9]
^[0-9]*.[0-9]*.[0-9]*.[0-9]*$
Change * to +:
^[0-9]+.[0-9]+.[0-9]+.[0-9]+$
Just a slight misunderstanding about * and +. The former accepts either no occurrences or more (>=0), the latter only matches if such vocabulary occurs at least once (>=1).
^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
I usually also escape the dot for safety, not entirely sure if it's necessary but I still do it. :)
You can use this pattern:
^\d+\.\d+\.\d+\.\d+$
Explanation:
^ - begin of string
\d - any digit
+ - at least one char
\. - exactly a dot char
$ - end of string
By the way, your input looks like an IP address. If so you can modify your reges like this:
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$
{1,3} means: 'from 1 to 3 chars'
Demo: regex101
To match 4 sets of digits separated by a period
^\d+(?:\.\d+){3}$
This question already has answers here:
Unrecognized escape sequence for path string containing backslashes
(5 answers)
Closed 7 years ago.
I'm trying to validate a password field using regex under the namespace System.Text.RegularExpressions but I'm getting three errors for
'Unrecognized escape sequence'.
When I double click on the errors it highlights the '-' in my expression for the character range but I don't know why this is wrong.
// password must contain one uppercase, one lowercase and one digit
Regex reg = new Regex("^(?=.*[!##$%^&*()\-_=+`~\[\]{}?|])(?=.+[a-z])(?=.+[A-Z])(? =.+[0-9]).{8,50}$");
Just add an # before the first quote to make it a verbatim string literal or escape the backslashes as \\.
it seems you have one space after ?
(? =.+[0-9]).{8,50}
remove that.