C# RegEx only replaces match if no other text in string - c#

I have the following RegEx pattern:
#"^((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$/gm"
It's supposed to replace strings such as:
10203040
10 20 30 40
+45 10 20 30 40
+4510203040
This is my replace method:
var text = "10 10 10 10";
text = Regex.Replace(text, #"^((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$/gm", "****");
The above code returns "****" which is correct.
var text = "10 10 10 10 10203040";
text = Regex.Replace(text, #"^((\(?\+45\)?)?)(\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{2})$/gm", "****");
The above code doesn't replace any text and just returns the original string - I need this code to return "**** ****", as there's two occurences of the numbers I need to match.
I hope someone can help me - thanks in advance :)

You've anchored your regex to start (^) and end ($) of line, so they will only perform replacements if the matched string is the entire line. Remove the anchors and it should work as expected.

Related

C# Regex Replace sequence of numbers preceded with a space

I have this string:
Hello22, I'm 19 years old
I just want to replace the number with * if its preceded with a space, so it would look like this:
Hello22, I'm ** years old
I've been trying a bunch of regexes but no luck. Hope someone can help out with the correct regex. Thank you.
Regexes which I tried:
Regex.Replace(input, #"([\d-])", "*");
Returns all numbers replaced with *
Regex.Replace(input, #"(\x20[\d-])", "*");
Does not work as expected
You can try (?<= )[0-9]+ pattern where
(?<= ) - look behind for a space
[0-9]+ - one or more digits.
Code:
string source = "Hello22, I'm 19 years old";
string result = Regex.Replace(
source,
"(?<= )[0-9]+",
m => new string('*', m.Value.Length));
Have a look at \b[0-9]+\b (here \b stands for word bound). This pattern
will substitute all 19 in the "19, as I say, 19, I'm 19" (note, that 1st 19 doesn't have space before it):
string source = "19, as I say, 19, I'm 19";
string result = Regex.Replace(
source,
#"\b[0-9]+\b",
m => new string('*', m.Value.Length));
In C# you could also make use of a pattern with a lookbehind and an infinite quantifier.
(?<= [0-9]*)[0-9]
The pattern matches:
(?<= Positive lookbehind, assert what is to the left of the current position is
[0-9]* Match a space followed by optional digits 0-9
) Close lookbehind
[0-9]\ Match a single digit 0-9
Example
string s = "Hello22, I'm 19 years old";
string result = Regex.Replace(s, "(?<= [0-9]*)[0-9]", "*");
Console.WriteLine(result);
Output
Hello22, I'm ** years old

Search for 2 specific letters followed by 4 numbers Regex

I need to check if a string begins with 2 specific letters and then is followed by any 4 numbers.
the 2 letters are "BR" so BR1234 would be valid so would BR7412 for example.
what bit of code do I need to check that the string is a match with the Regex in C#?
the regex I have written is below, there is probably a more efficient way of writing this (I'm new to RegEx)
[B][R][0-9][0-9][0-9][0-9]
You can use this:
Regex regex = new Regex(#"^BR\d{4}");
^ defines the start of the string (so there should be no other characters before BR)
BR matches - well - BR
\d is a digit (0-9)
{4} says there must be exactly 4 of the previously mentioned group (\d)
You did not specify what is allowed to follow the four digits. If this should be the end of the string, add a $.
Usage in C#:
string matching = "BR1234";
string notMatching = "someOther";
Regex regex = new Regex(#"^BR\d{4}");
bool doesMatch = regex.IsMatch(matching); // true
doesMatch = regex.IsMatch(notMatching); // false;
BR\d{4}
Some text to make answer at least 30 characters long :)

Regex format returns empty result - C#

I have below text line and I intend to extract the "date" after the ",", i,e,
1 Sep 2015
Allocation/bundle report 10835.0000 Days report step 228, 1 Sep 2015
I wrote the below regex code and it returns empty in the match.
`Regex regexdate = new Regex(#"\Allocation/bundle\s+\report\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\,\+(\S)+\s+(\S)+\s+(\S)"); // to get dates
MatchCollection matchesdate = regexdate.Matches(text);
Can you advice about what's wrong with the Regex format that I mentioned?
The \A is an anchor asserting the start of string. You must have meant A. (\S)+ must be turned into (\S+). Also, \r is a carriage return matching pattern, again remove the backslash to turn \r into r.
Use
#"Allocation/bundle\s+report\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\,\s+(\S+)\s+(\S+)\s+(\S+)"
See the regex demo
Note that the last part of the regex may be made a bit more specific to match 1+ digits, then some letters and then 4 digits: (\S+)\s+(\S+)\s+(\S+) -> (\d+)\s+(\p{L}+)\s+(\d{4})
Can you do it without Regex? Here's an example using a bit of help from LINQ.
var text = "Allocation/bundle report 10835.0000 Days report step 228, 1 Sep 2015";
var sDate = text.Split(',').Last().Trim();
if (string.IsNullOrEmpty(sDate))
{
Console.WriteLine("No date found.");
}
else
{
Console.WriteLine(sDate); // Returns "1 Sep 2015"
}

C# regular expression match triple quotes """

I have a text file that contain 3 quotations (""") in various lines in text. It also have 6 blank spaces before that in every line.
I have tried doing #"\s{6}\"{3}"; and various cases, but it seems like c# doesn't like when it sees 3 quotations mark together. What I'm trying to do is to find that and add a new line after.
This is what i have tried:
string pattern4 = #"\s{6}"{3}";
var match4 = Regex.Match(body, pattern4, RegexOptions.Multiline);
while (match4.Success)
{
string index = """;
output.Insert(index, "\r\n");
}
Sample Input:
"""Step: 33 And I enter
Step: 34 And I set the
Desired Output:
"""
Step: 33 And I enter
Step: 34 And I set THE
To escape a quote inside a verbatim string (starts with #) use double quotes. Also there is a Regex.Replace method that you could use like this:
string input = #" """"""Step: 33 And I enter
Step: 34 And I set the ";
string pattern = #"\s{6}""{3}";
string replacement = "\"\"\"\r\n";
string output = Regex.Replace(input, pattern, replacement);

match regex patten between two pattern

Hi I am newbie in RegEx operations. I have a text like
[JUNCTIONS]
;ID Elev Demand Pattern
3 50 100 ;
4 50 30 ;
5 50 20 ;
6 40 20 ;
7 50 5 ;
8 30 5 ;
9 30 5 ;
2 50 80 ;
10 50 70 ;
11 50 30 ;
12 50 52 ;
13 50 40 ;
14 50 40 ;
15 50 10 ;
16 50 10 ;
17 50 10 ;
18 0 0 ;
19 0 0 ;
[RESERVOIRS]
;ID Head Pattern
1 100 ;
[TANKS]
I want to create a pattern and output the text between [JUNCTIONS] and [RESERVOIRS] then [RESERVOIRS] to [TANKS] then so on. [XXXX] is not known to me. I want to get text inside [XXX] to [XXX]. How can i do that?
Here is the regex:
(?=(\[\S+\].*?\[\S+\]))
or
(?=(\[(?:JUNCTIONS|RESERVOIRS)\].*?\[(?:RESERVOIRS|TANKS)\]))
Assuming you want to handle all the [...] things from your input.
Note: Use the make sure you are handling multiple line regex matching from your c#. And don't for get to escape the \ character if you need.
Here is some c# code to do the match, and get the results.
Be sure to add error checking, for example to make sure that the match actually worked.
Note the Singleline flag - this lets the dot (.) match all characters, including newlines. You'll also probably need to cleanup and trim the output, to remove any trailing newlines, etc.
MatchCollection matches = Regex.Matches(test, #"^\[JUNCTIONS\](.*)\[RESERVOIRS\](.*)\[TANKS\](.*)$", RegexOptions.Singleline);
GroupCollection groups = matches[0].Groups;
// JUNCTIONS text
Console.WriteLine(groups[1]);
// RESERVOIRS text
Console.WriteLine(groups[2]);
Edit - Updated to match OP's changes
If you want to match an unspecified number of matches, its a little trickier. This regex will match a [TEXT] block and anything that comes after it, until it its a [ character. The way to use this regex is to loop over the MatchCollection for each region, and use .groups[1] for the text and .groups[2] for the body.
MatchCollection matches =
Regex.Matches(test, #"\[([\w+]+)\]([^\[]+)?", RegexOptions.Singleline);
// for each block / section of the document
foreach(Match match in matches){
GroupCollection groups = match.Groups;
// [TEXT] part will be here
Console.WriteLine(groups[1]);
// The rest will be here
Console.WriteLine(groups[2]);
}
Why use a regex?
Assuming you can read this input text one line at a time, it will probably be quicker and easier to just loop over the lines, and output those you need. Some variant of:
Update:
In response to you comment below; you can probably use this to skip any lines with [something] in them, and print out the rest:
// Pattern: Any instance of [] with one or more characters of between them:
var pattern = #"\[.+\]";
while((line = file.ReadLine()) != null)
{
if(!Regex.IsMatch(line, pattern)) // Skip lines that match
{
Console.WriteLine(line);
}
}

Categories