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.
Related
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".
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
I am trying to match everything between
/* and */
And also include the in between characters.
I currently managed to create a pattern that kind of does this
\/\*(.+?)\*\/
Regex Tester
But it doesn't match multi line quotes and only matches once.
How can I improve this pattern to match everything that starts with /* and ends with */ ?
You need the RegexOptions.Singleline option, which makes the . match newlines.
Regex rx = new Regex("/\*(.+?)\*/", RegexOptions.Singleline);
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:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I need to get all substrings that are placed between 2 signs.
For example substrings placed between ] and [:
abcabc]substrings[kkkkkkk]iwant[12345]tohave[!##$%]
and I get: substrings iwant tohave
I tried (?<=\])(.*)(?=\[) but it returns substrings[kkkkkkk]iwant[12345]tohave.
Your regex would need to be (?<=\])(.*?)(?=\[).
Note the added ? sign to match as few as possible.
Then you have to combine the (at the moment) three matches with spaces and you will get the output you want!
Make it non greedy .*? or else it would match until the last [
You don't need the capturing group if you want to get the matches only:
(?<=\]).*?(?=\[)
Test
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.