C# Regular Expression - find groups in text with a separator [duplicate] - c#

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I have the following text
"a|mother" "b|father"
I want to find via Regex, groups of text that starts with '"' and ends with '"' and separate with '|' without spaces. Meaning the results would be:
"a|mother"
"b|father"
I try to use other posts to solve my question but still with no luck how can I find the |? and how can I find my pattern without spaces?

Something like this:
String source = "\"a|mother\" \"b|father\"";
var result = Regex
.Matches(source, "\"[^\"]*[^ ]\\|[^ ][^\"]*\"")
.OfType<Match>();
Console.Write(String.Join(Environment.NewLine, result));
Output is
"a|mother"
"b|father"

Related

Simple regex for email validation using C#. Has # symbol in the middle [duplicate]

This question already has answers here:
What's wrong with this RegEx for validating emails?
(5 answers)
Closed 2 years ago.
I don't know anything about regex.
How do I create an email validation that allows such entries?
Allow
a1b2c3#a1b2c3
a#a
1#1
a1#a#a2e
!s#s$ds
Dont Allow
#
###
!##
Basically allow characters that has # in the middle
If you aren't fluent in regex you can split the string at "#" and expect two elements.
If you want to use regex, the first result on google was: https://emailregex.com

How to get all possible Regex Matches [duplicate]

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

c# regex extracting substrings from a string [duplicate]

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

regex expression to accept ENTER key in string [duplicate]

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.

How capture nested groups? [duplicate]

This question already has answers here:
What are regular expression Balancing Groups?
(2 answers)
Closed 8 years ago.
I have a target string like this: (however nesting can be very deep in practice)
{hi {how {are {you}}}}
Desired result would be:
Groups:
hi how are you
I can't find anything in c# regex to do nested capturing like that. Is it possible at all?
EDIT:
I think I simplified my example too much which obscures the answers.
I need to capture in a recursive sort of way because I need the content inside the brackets:
{test[{test2[content]}]}
where the desired result would be:
{test2[content]} and content
{([^{}]+)
Try this.See demo.grab the captures.
http://regex101.com/r/oE6jJ1/35
I could think of this:
string balh = "{hi {how {are {you}}}}";
string[] foo = balh.Split(new char[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries);
string output = string.Join(" ", foo);
The groups will be in foo array.
EDIT:
I think you are looking for more elaborate inputs. I got the question wrong :(

Categories