Regex for text between two characters - c#

I'm trying to get some text between two strings in C# in regex expression.
The text is in variable (tb1.product_name) : Example Text | a:10,Colour:Green
Get all text before |, in this case, Example Text
Get all text between : and ,, in this case, 10
In two differents regex.
I try with:
Regex.Match(tb1.product_name, #"\:([^,]*)\)").Groups[1].Value
But this doesn't work.

If it is not so necessary to use regex, you can do this simply by using string.Substring & string.IndexOf:
string str = "Example Text | a:10,Colour:Green";
string strBeforeVerticalBar = str.Substring(0, str.IndexOf('|'));
string strInBetweenColonAndComma = str.Substring(str.IndexOf(':') + 1, str.IndexOf(',') - str.IndexOf(':') - 1);
Edit 1:
I feel Regex might be an overkill for something as simple as this. Also if use what i suggested, you can add Trim() at the end to remove whitespaces, if any. Like:
string strBeforeVerticalBar = str.Substring(0, str.IndexOf('|')).Trim();
string strInBetweenColonAndComma = str.Substring(str.IndexOf(':') + 1, str.IndexOf(',') - str.IndexOf(':') - 1).Trim();

string str = #"Example Text |a:10,Colour: Green";
Match match = Regex.Match(str, #"^([A-Za-z\s]*)|$");
Match match2= Regex.Match(str, #":([0-9]*),");
//output Example Text
Console.WriteLine(match.Groups[1].Value);
//output 10
Console.WriteLine(match2.Groups[1].Value);

Related

Regex extraction of a specific pattern

I have a string of following format. I have three scenarios which follows as:
Scenario 1:
"\\hjsschjsn\Bunong.PU2.PV/-56Noogg.BSC";
The extraction should be until ".BSC" , ".BSC" will be there in the original string always. Also "\" and "\" will be there but the text will change.
I have to omit the middle part , my output should be :
"\\hjsschjsn\-56Noogg.BSC";
Scenarion 2:
"\\adajsschjsn\Bcscx.sdjhs\AHHJogg.BSC";
The output should be :
"\\adajsschjsn\AHHJogg.BSC";
Scenario 3:
"aasjkankn\\adajsschjsn\Bcscx.sdjhs\AHHJogg.BSC\djkhakdjhjkj";
output should be:
"\\adajsschjsn\AHHJogg.BSC";
Here's what I have tried:
string text = "\\\\hjsschjsn\Bunong.PU2.PV/-56Noogg.BSC";
//Note: I have given \\\\ instead of \\ because of string literal to be accomadated in a string
Match pattern = Regex.Match(text, #"\\\\[\w]+\\/[\w*]+.BSC");
Try following mask:
.*(\\\\[^\\]*\\)([^\\\/]+)[\\\/](.*?\.BSC).*
Replace it with $1$3
Regex reg = new Regex(#".*(\\\\[^\\]*\\)([^\\\/]+)[\\\/](.*?\.BSC).*");
string input = #"\\hjsschjsn\Bunong.PU2.PV/-56Noogg.BSC";
string output = reg.Replace(input, "$1$3");
See example here
Match pattern1 = Regex.Match(text, #"\\\\\w+\\");
Match pattern2 = Regex.Match(text, #"\w+.BSC");
Console.WriteLine(pattern1.ToString() + pattern2.ToString());

Regex to match only numbers , no apostrophes

I want to match only numbers in the following string
String : "40’000"
Match : "40000"
basically tring to ignore apostrophe.
I am using C#, in case it matters.
Cant use any C# methods, need to only use Regex.
Replace like this it replace all char excpet numbers
string input = "40’000";
string result = Regex.Replace(input, #"[^\d]", "");
Since you said; I just want to pick up numbers only, how about without regex?
var s = "40’000";
var result = new string(s.Where(char.IsDigit).ToArray());
Console.WriteLine(result); // 40000
I suggest use regex to find the special characters not the digits, and then replace by ''.
So a simple (?=\S)\D should be enough, the (?=\S) is to ignore the whitespace at the end of number.
DEMO
Replace like this it replace all char excpet numbers and points
string input = "40’000";
string result = Regex.Replace(input, #"[^\d^.]", "");
Don't complicate your life, use Regex.Replace
string s = "40'000";
string replaced = Regex.Replace(s, #"\D", "");

C# How to replace a shorter string than the matched string?

How can I replace only a part of a matched regex string ? I need to find some strings that are inside of some brackets like < >. In this example I need to match 23 characters and replace only 3 of them:
string input = "<tag abc=\"hello world\"> abc=\"whatever\"</tag>";
string output = Regex.Replace(result, ???, "def");
// wanted output: <tag def="hello world"> abc="whatever"</tag>
So I either need to find abc in <tag abc="hello world"> or find <tag abc="hello world"> and replace just abc. Do regular expressions or C# allow that ? And even if I solve the problem differently is it possible to match a big string but replace only a little part of it ?
I'd have to look up the #NET regex dialect, but in general you want to capture the parts you don't want to replace and refer to them in your replacement string.
string output = Regex.Replace(input, "(<tag )abc(=\"hello world\">)", "$1def$2");
Another option would be to use lookaround to match "abc" where it follows "<tag " and precedes "="hello world">"
string output = Regex.Replace(input, "(?<=<tag )abc(?==\"hello world\")", "def");
Instead of Regex.Replace use Regex.Match, then you can use the properties on the Match object to figure out where the match occurred.. then the regular string functions (String.Substring) can be used to replace the bit you want replaced.
Working sample with named groups:
string input = #"<tag abc=""hello world""> abc=whatever</tag>";
Regex regex = new Regex(#"<(?<Tag>\w+)\s+(?<Attr>\w+)=.*?>.*?</\k<Tag>>");
string output = regex.Replace(input, match =>
{
var attr = match.Groups["Attr"];
var value = match.Value;
var left = value.Substring(0, attr.Index);
var right = value.Substring(attr.Index + attr.Length);
return left + attr.Value.Replace("abc", "def") + right;
});

Getting a value from a string using regular expressions?

I have a string "Page 1 of 15".
I need to get the value 15 as this value could be any number between 1 and 100. I'm trying to figure out:
If regular expressions are best suited here. Considering string will never change maybe just split the string by spaces? Or a better solution.
How to get the value using a regular expression.
Regular expression you can use: Page \d+ of (\d+)
Regex re = new Regex(#"Page \d+ of (\d+)");
string input = "Page 1 of 15";
Match match = re.Match(input);
string pages = match.Groups[1].Value;
Analysis of expression: between ( and ) you capture a group. The \d stands for digit, the + for one or more digits. Note that it is important to be exact, so copy the spaces as well.
The code is a tad verbose, but I figured it'd be better understandable this way. With a split you just need: var pages = input.Split(' ')[3];, looks easier, but is error-prone. The regex is easily extended to grab other parts of the string in one go.
var myString = "Page 1 of 15";
var number = myString.SubString(myString.LastIndexOf(' ') + 1);
If there is a risk of whitespace at the end of the string then apply a TrimEnd method:
var number = myString.SubString(myString.TrimEnd().LastIndexOf(' ') + 1);
I think a simple String.Replace() is the best, most readable solution for something so simple.
string myString = "Page 1 of 15";
string pageNumber = myString.Replace("Page 1 of ", "");
EDIT:
The above solution assumes that the string will never be Page 2 of 15. If the first page number can change, you'll need to use String.Split() instead.
string myString = "Page 1 of 15";
string pageNumber = myString.Split(new string[] {"of"},
StringSplitOptions.None).Last().Trim();
if the string format will never change then you can try this...
string input = "Page 1 of 15";
string[] splits = input.Split(' ');
int totalPages = int.Parse(splits[splits.Length - 1]);
If this is the only case you will ever have to handle, just split the string by spaces and use the parse the 4th part to an integer. Something like that will work:
string input = "Page 1 of 15";
string[] splitInput = string.Split(' ');
int numberOfPages = int.Parse(splitInput[3]);
In c# it should looks like this (using Regex class):
Regex r = new Regex(#"Page \d+ of (\d+)");
var str = "Page 1 of 15";
var mathes = r.Matches(str);
Your resoult will be in: mathes[0].Groups[1]
You dont need a regex for this. Just the the index of the last space
string var = "1 of100";
string secondvar = string.Empty;
int startindex = var.LastIndexOf(" ");
if (startindex > -1)
{
secondvar = var.Substring(startindex +1);
}

Quantity of specific strings inside a string

I'm working in .net c# and I have a string text = "Whatever text FFF you can FFF imagine";
What i need is to get the quantity of times the "FFF" appears in the string text.
How can i acomplished that?
Thank you.
You can use regular expressions for this and right about anything you want:
string s = "Whatever text FFF you can FFF imagine";
Console.WriteLine(Regex.Matches(s, Regex.Escape("FFF")).Count);
Here are 2 approaches. Note that the regex should use the word boundary \b metacharacter to avoid incorrectly matching occurrences within other words. The solutions posted so far do not do this, which would incorrectly count "FFF" in "fooFFFbar" as a match.
string text = "Whatever text FFF you can FFF imagine fooFFFbar";
// use word boundary to avoid counting occurrences in the middle of a word
string wordToMatch = "FFF";
string pattern = #"\b" + Regex.Escape(wordToMatch) + #"\b";
int regexCount = Regex.Matches(text, pattern).Count;
Console.WriteLine(regexCount);
// split approach
int count = text.Split(' ').Count(word => word == "FFF");
Console.WriteLine(count);
Regex.Matches(text, "FFF").Count;
Use the System.Text.RegularExpressions.Regex for this:
string p = "Whatever text FFF you can FFF imagine";
var regex = new System.Text.RegularExpressions.Regex("FFF");
var instances = r.Matches(p).Count;
// instances will now equal 2,
Here's an alternative to the regular expressions:
string s = "Whatever text FFF you can FFF imagine FFF";
//Split be the number of non-FFF entries so we need to subtract one
int count = s.Split(new string[] { "FFF" }, StringSplitOptions.None).Count() - 1;
You could easily tweak this to use several different strings if necessary.

Categories

Resources