c# regexp , replacing only in part of text - c#

I have a text variable which can contain many ((VAR=var_name)).
I want to replace ((VAR=var_name)) by the var_name only in src= (so, no in href= for instance.
sText = "some text href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext((VAR=XXXX))sometext((VAR=YYYYY)) \" some text some text
href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\" some text";
I want to get after replacing :
sText = "some text href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext**XXXX**sometext**YYYYY**\" some text some text
href=\"sometext((VAR=XXXX))sometext((VAR=YYYYY))\"
src=\"sometext**XXXX**sometext**YYYYY**\" some text";
Have you got an idea ?
JC

string srcPattern = #"src="".+?""";
string varPattern = #"\(\(VAR=(.+?)\)\)";
string result = Regex.Replace(sText, srcPattern,
m => Regex.Replace(m.Value, varPattern, n => "**" + n.Groups[1].Value + "**"));
The first regular expression match the string src = "...". While the second regular expression replaces every statement in brackets on its value.

Related

Text between 2 optional strings with OR condition using Regex

I have a string with 2 possibilities:
var desc = "Keyword1: That text I want \r\n Keyword2: Value2 \r\n Keyword3: Value3 \r\n Keyword4: Value4"
var desc = "Keyword1: That text I want Keyword2: Value2 \r\n Keyword3: Value3 \r\n Keyword4: Value4"
where the order of the keywords after the text "That text I want" Keyword2, Keyword3, Keyword4 doesn't matter and they are all optional.
I tried with the Regex Keyword1:(\s+)(.*)(\W+?)(\r\n?)(?=Keyword2:|Keyword3:|Keyword4:)
It does not work. Not sure what is wrong in my regex.
Any help is highly appreciated.
Thanks in advance!
Show here for the solution.
In your case you could simply use (regex between two strings):
(?<=Keyword1:)(.*)(?=Keyword2)
Try it out
Hope it helps.
Assuming those \r\n are actual special characters in the string and not the literals, this should work:
Keyword1: (.*?)(Keyword2:|Keyword3:|Keyword4:|\r\n)
You need to get the second grouping from the match. For example: match.Groups[1].
This regex matches Keyword1:, followed by the minimum amount of necessary characters, and then followed by either Keyword2: or \r\n (special characters). If those are literals in your input string, you will need to double those backslashes.
You can check it here. Note that on the right, Group 1 contains your text in both cases.
var pattern = keywordName + #":\s+(.+?)\r?\n";
var regex = new Regex(pattern);
var match = regex.Match(description);
if (!match.Success) return null;
var firstMatch = match.Groups[1].Value;
//Find if there's another keyword in the extracted Value
var lstKeywords = Enum.GetValues(typeof(Keywords)).Cast<Keywords>().Where(k => k != keywordName);
//Add : to the last value so that it's recognized as a keyword
var sOtherKeywords = string.Join(":|", lstKeywords) + ":";
var pattern2 = #"(" + sOtherKeywords + #")(\s+)";
regex = new Regex(pattern2);
match = regex.Match(firstMatch);
//If there's no other keyword in the same line then return the expression that is extracted from the first regex
if (!match.Success) return firstMatch;
var secondMatch = match.Groups[1].Value;
var pattern3 = keywordName + #":\s+(.+)(\r?\n?)" + secondMatch;
regex = new Regex(pattern3);
match = regex.Match(description);
return match.Success ? match.Groups[1].Value.TrimEnd() : null;

C# - how can i convert “text text 542050.0000 text text” to “text text 542050 text text”?

How can I convert the following string:
string x = "text text 542050.0000 text 245.00 text";
to this: "text text 542050 text 245 text"
i want to keep all text in this string , just remove decimal part of numbers
If you want to solve for the general case of removing decimals in strings, you can use RegEx:
var input = "text text 542050.0000 text text";
var regex = "((?<keep>[0-9]+)\\.[0-9]+)";
var matchEvaluator = new System.Text.RegularExpressions.MatchEvaluator((m) => m.Groups["keep"].Value);
var output = System.Text.RegularExpressions.Regex.Replace(input, regex, matchEvaluator);
The RegEx will match all decimals, and return the whole part as a replacement. Note that if you have a string like 5.2.1 then this will result in "5.1".
If you want to output a specific number of decimal places you could try this:
var input = "text text 542050.0129 text text";
var regex = "([0-9]+\\.[0-9]+)";
var matchEvaluator = new System.Text.RegularExpressions.MatchEvaluator((m) => {
var decimalValue = double.Parse(m.Groups[0].Value);
return String.Format("{0:0.000}", Math.Round(decimalValue, 3));
});
var output = System.Text.RegularExpressions.Regex.Replace(input, regex, matchEvaluator);
In my example, I've rounded to 3 dp and formatted with 3 dp (formatting to ensure that it always outputs .000 - you can remove this step if you don't need it). For "542050.0129" above, it would output "542050.013"

Add space around each / using Humanizer or Regex

I have a string like the following:
var text = #"Some text/othertext/ yet more text /last of the text";
I want to normalize the spaces around each slash so it matches the following:
var text = #"Some text / othertext / yet more text / last of the text";
That is, one space before each slash and one space after. How can I do this using Humanizer or, barring that, with a single regex? Humanizer is the preferred solution.
I'm able to do this with the following pair of regexes:
var regexLeft = new Regex(#"\S/"); // \S matches non-whitespace
var regexRight = new Regex(#"/\S");
var newVal = regexLeft.Replace(text, m => m.Value[0] + " /");
newVal = regexRight.Replace(newVal, m => "/ " + m.Value[1]);
Are you looking for this:
var text = #"Some text/othertext/ yet more text /last of the text";
// Some text / othertext / yet more text / last of the text
string result = Regex.Replace(text, #"\s*/\s*", " / ");
slash surrounded by zero or more spaces replaced by slash surrounded by exactly one space.

Replace after a string - Before a character

I have a string like :
string str = "First Option: This is a text. This is second text.";
I can replace This is a text. with :
str = str.Replace("This is a text.", "New text");
But my constant word is First Option: and This is a text is not constant so how can replace the text after First Option: until occurring . (it means before This is second text.).
In this example the expecting result is :
First Option: New text. This is second text.
One option is to use Regex.Replace instead:
str = Regex.Replace(str, #"(?<=First Option:)[^.]*", "New text");
(?<=First Option:)[^.]* matches a sequence of zero or more characters other than dot '.', preceded by First Option: via a positive look-behind.
Not the shortest but if you want to avoid regular expressions:
string replacement = "New Text";
string s = "First Option: This is a text.This is second text.";
string[] parts = s.Split('.');
parts[0] = "First Option: " + replacement;
s = string.Join(".", parts);
Look up .IndexOf() and Substring(...). That will give you what you need:
const string findText = "First Option: ";
var replaceText = "New Text.";
var str = "First Option: This is a text. This is second text.".Replace(findText, "");
var newStr = findText + str.Replace(str.Substring(0, str.IndexOf(".") + 1), replaceText);
Console.WriteLine(newStr);

Replacing unknown text between " " with #

I currently have a large text file and I am trying to replace unknown text between anything inside "" with #. I've tried to use:
string text = File.ReadAllText(#"c:\Users\Zero\Documents\test.txt");
string replacement = "#";
int start = text.IndexOf('"') + 1;
text = text.Replace(text.Substring(start, text.LastIndexOf('"') - start), replacement);
File.WriteAllText(#"c:\Users\Zero\Documents\test.txt", text);
Currently it's replacing all text in the file with one line. in other words, it's turning:
Hi how are you "test"
This is a test "123" test
"test" "test"
into
"#"
I need it to do this
Hi how are you "#"
This is a test "#" test
"#" "#"
A regular expression like this would come in handy:
"[^"]*"
For example:
text = Regex.Replace(text, "\"[^\"]*\"", "\"#\"");

Categories

Resources