Match everything between characters in Regex? [duplicate] - c#

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);

Related

Why does regex detect newline as a valid newline but not \n? [duplicate]

This question already has answers here:
Regex that matches a newline (\n) in C#
(5 answers)
Closed 3 years ago.
I'm currently trying to test out a regex for a project I'm working on, I've found a problem while doing it. (these are C# regexes from System.Text.RegularExpression)
The input:
11
w
Pattern 1:
\d{1,}
\w
Pattern 2:
\d{1,}\n\w
in this page that I found, it states that \n matches a new line character (and in any other page I could looked at)
so if that's the case, why does pattern 1 match the input but pattern 2 not? shouldn't they be the same?
from what Tim Biegeleisen said, "on windows the line separator is not just \n, it's \r\n", with this I changed pattern 2 to \d{1,}\r\n\w and everything turned out fine.

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

Use RegEx to find 100$ in text c# [duplicate]

This question already has answers here:
C# Regex Match whole word, with special characters
(2 answers)
Regex whitespace word boundary
(3 answers)
Closed 4 years ago.
I am trying to search 100$ through the RegEx. But unable to find its pattern is as above.
\b100\$\b
However, if the text contains 100$1 then it displays properly.
But I want to do exact search.
Word boundaries only work with word characters, not $. Try using this regex:
\b100\$(?=\s|\$)
This will match 100$, where what follows the $ is either whitespace or the end of the line.
Demo

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

how to match "test\w*" but not "tester" with a single regex expression [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 8 years ago.
Say I have the following string:
test tester tested testing
I wish to match every word that begins with test, but not the word tester. The pattern test\w* does most of the job, but I have no idea how to add that tester should not be matched. A working pattern should give 3 matches on the string.
Does this work for your purposes?
test(?!er).*

Categories