i have some string like the ones below:
hu212 text = 1
reference = 1
racial construction = 1
2007 = 1
20th century history = 2
and i want to take only the integer AFTER the '='.. how can i do that?
i am trying this:
Regex exp = new Regex(#"[a-zA-Z]*[0-9]*[=][0-9]+",RegexOptions.IgnoreCase);
try
{
MatchCollection MatchList = exp.Matches(line);
Match FirstMatch = MatchList[0];
Console.WriteLine(FirstMatch.Value);
}catch(ArgumentOutOfRangeException ex)
{
System.Console.WriteLine("ERROR");
}
but it is not working...
i tryed some others but i get results like "20th" or "hu212"...
What exaclty Matches does? gives me the rest of the string that doesn match with the reg?
Instead of Regex you could also do:
int match = int.Parse(line.SubString(line.IndexOf('=')).Trim());
You need to allow whitespace (\s) between the = and the digits:
Regex pattern = new Regex(#"=\s*([0-9]+)$");
Here's a more complete example:
Regex pattern = new Regex(#"=\s*([0-9]+)$");
Match match = pattern.Match(input);
if (match.Success)
{
int value = int.Parse(match.Groups[1].Value);
// Use the value
}
See it working online: ideone
what about
string str = "hu212 text = 1"
string strSplit = str.split("=")[1].trim();
String StringToParse = "hu212 text = 1";
String[] splitString = String.Split(StringToParse);
Int32 outNum;
Int32.TryParse ( splitString[splitString.Length-1], out outNum );
Regex pattern = new Regex(#"=\s?(\d)");
This allow to have with or without space. The number is in group 1.
hu212 text =1
reference = 1
Related
534-W1A-R1 this is my file name and I want to split it so it prints like
Code=534 Phase=1 Zone=A
in my Autocad file.
The below split code should work:
string str = #"534-W1A-R1";
var split = str.Split('-');
string code = split.First();
string phase = new string(split.ElementAt(1).Skip(1).Take(1).ToArray());
string zone = new string(split.ElementAt(1).Skip(2).Take(1).ToArray());
string result = String.Format("Code={0} Phase={1} Zone={2}", code, phase, zone);
Console.WriteLine(result);
Output:
Code=534 Phase=1 Zone=A
Use the Substring() method.
string input = "534-W1A-R1";
string sub = input.Substring(0, 3);
string sub2 = input.Substring(5, 1);
string sub3 = input.Substring(6, 1);
Console.WriteLine("Code={0} Phase={1} Zone={2}", sub, sub2, sub3);
Output:
Code=534 Phase=1 Zone=A
You have different ways to do it. if you are sure about the format of the text you can just use this:
var str= "534-W1A-R1";
var parts=str.Split('-');
var code= parts[0];
var secondPart= parts[1];
var phase=secondPart.Substring(1,secondPart.Length-2);
var zone=secondPart[secondPart.Length-1];
You can also use Regex if it is more complicated.
Using Regex
Edit: added some comments (pattern description)
var pattern = #"^(\d+)-[A-Z](\d+)([A-Z])-";
/* pattern description:
^(\d+) group 1: one or more digits at the begining
- one hyphen (literal)
[A-Z] one alphabetic character
(\d+) group 2: one or more digits
([A-Z]) group 3: one alphabetic character
- one hyphen (literal)
*/
var input = "534-W1A-R1";
var groups = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Groups;
var code = groups[1].Value;
var phase = groups[2].Value;
var zone = groups[3].Value;
I have a string in my c#:
The.Big.Bang.Theory.(2013).S07E05.Release.mp4
I need to find an occurance of (2013), and replace the whole thing, including the brackets, with _ (Three underscores). So the output would be:
The.Big.Bang.Theory._.S07E05.Release.mp4
Is there a regex that can do this? Or is there a better method?
I then do some processing on the new string - but later, need to report that '(2013)' was removed .. so I need to store the value that is replaced.
Tried with your string. It works
string pattern = #"\(\d{4}\)";
string search = "The.Big.Bang.Theory.(2013).S07E05.Release.mp4";
var m = Regex.Replace(search, pattern, "___");
Console.WriteLine(m);
This will find any 4 digits number enclosed in open/close brakets.
If the year number can change, I think that Regex is the best approach .
Instead this code will tell you if there a match for your pattern
var k = Regex.Matches(search, pattern);
if(k.Count > 0)
Console.WriteLine(k[0].Value);
Many of these answers forgot the original question in that you wanted to know what you are replacing.
string pattern = #"\((19|20)\d{2}\)";
string search = "The.Big.Bang.Theory.(2013).S07E05.Release.mp4";
string replaced = Regex.Match(search, pattern).Captures[0].ToString();
string output = Regex.Replace(search, pattern, "___");
Console.WriteLine("found: {0} output: {1}",replaced,output);
gives you the output
found: (2013) output: The.Big.Bang.Theory.___.S07E05.Release.mp4
Here is an explanation of my pattern too.
\( -- match the (
(19|20) -- match the numbers 19 or 20. I assume this is a date for TV shows or movies from 1900 to now.
\d{2} -- match 2 more digits
\) -- match )
Here is a working snippet from a console application, note the regex \(\d{4}\):
var r = new System.Text.RegularExpressions.Regex(#"\(\d{4}\)");
var s = r.Replace("The.Big.Bang.Theory.(2013).S07E05.Release.mp4", "___");
Console.WriteLine(s);
and the output from the console application:
The.Big.Bang.Theory.___.S07E05.Release.mp4
and you can reference this Rubular for proof.
Below is a modified solution taking into consideration your additional requirement:
var m = r.Match("The.Big.Bang.Theory.(2013).S07E05.Release.mp4");
if (m.Success)
{
var s = "The.Big.Bang.Theory.(2013).S07E05.Release.mp4".Replace(m.Value, "___");
var valueReplaced = m.Value;
}
Try this:
string s = "The.Big.Bang.Theory.(2013).S07E05.Release.mp4";
var info = Regex.Split(
Regex.Matches(s, #"\(.*?\)")
.Cast<Match>().First().ToString(), #"[\s,]+");
s = s.Replace(info[0], "___");
Result
The.Big.Bang.Theory.___.S07E05.Release.mp4
try this :
string str="The.Big.Bang.Theory.(2013).S07E05.Release.mp4";
var matches = Regex.Matches(str, #"\([0-9]{4}\)");
List<string> removed=new List<string>();
if (matches.Count > 0)
{
for (int i = 0; i < matches.Count; i++)
{
List.add(matches.value);
}
}
str=Regex.replace(str,#"\([0-9]{4}\)","___");
System.out.println("Removed Strings are:")
foreach(string s in removed )
{
System.out.println(s);
}
output:
Removed Strings are:
(2013)
You don't need a regex for a simple replace (you can use one, but's it's not needed)
var name = "The.Big.Bang.Theory.(2013).S07E05.Release.mp4";
var replacedName = name.Replace("(2013)", "___");
I'm writing a c# code that divide a string into two different groups. a string is pipe-delimited as example below:
there could be an empty space between two pipes.
number of pipes to "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w" are fixed; In this case, there are 4 pipes.
string value = "122312121|test value||test value 2|5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w|123456789|123456789";
const string sPattern = #"What should it be here?????";
var regex = new Regex(sPattern);
var match = regex.Match(value);
if (match.Success)
{
var begin = match.Groups["begin"].Value;
var middle = match.Groups["middle"].Value;
var end = match.Groups["end"].Value;
}
I am trying to get the output of the code to return as following:
begin = "122312121|test value||test value 2|"
middle = "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w"
end = "|123456789|123456789"
However, I'm so new to regular expression, and I have tried to write a regular expression for variable sPattern, but could not produce the right regular expression for it. Could any please help? Thanks.
you should use String.Split
string [] sarray = value.Split('|')
What that will do is give you the array
{"122312121", "test value", "" , "test value" , "2", "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w", "123456789", "123456789"}
and 5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w will be in sarray[5]
If you're looking for a regular expression to match this and want to use a regular expression rather than .Split, you could try this:
"^((.*?[|]){4})(.*?)([|].*)*$"
or more explicitly:
"^(?<begin>(.*?[|]){4})(?<middle>.*?)(?<end>[|].*)*$"
This is based on the fact that you said the number of pipes before your long string is fixed (at four).
Your code would then read as follows:
string value = "122312121|test value||test value 2|5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w|123456789|123456789";
const string sPattern = #"^((.*?[|]){4})(.*?)([|].*)*$";
var regex = new Regex(sPattern);
var match = regex.Match(value);
if (match.Success)
{
var begin = match.Groups[1].Value;
var middle = match.Groups[3].Value;
var end = match.Groups[4].Value;
}
The trick may be to escape the pipe character:
const string sPattern = #"(?<begin>[^|]*\|[^|]*\|[^|]*\|[^|]*\|)" +
"(?<middle>[^|]*)" +
"(?<end>\|.*)";
You could use String.Split and some Linq to do what you need
Rough example:
string value = "122312121|test value||test value 2|5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w|123456789|123456789";
string[] split = value.Split('|');
string begin = string.Join("|", split.Take(4));
string middle = split.Skip(4).Take(1).FirstOrDefault();
string end = "|" + string.Join("|", split.Skip(5).Take(2));
Returns
begin = "122312121|test value||test value 2|"
middle = "5GOdNF7Q5fK5O9QKiZefJEfO1YECcX1w"
end = "|123456789|123456789"
Here's another one:
^(?<begin>(.*?\|){4})(?<middle>.*?(?=\|))(?<end>.*)
I have a list of string
goal0=1234.4334abc12423423
goal1=-234234
asdfsdf
I want to extract the number part from string that start with goal,
in the above case is
1234.4334, -234234
(if two fragments of digit get the first one)
how should i do it easily?
Note that "goal0=" is part of the string, goal0 is not a variable.
Therefore I would like to have the first digit fragment that come after "=".
You can do the following:
string input = "goal0=1234.4334abc12423423";
input = input.Substring(input.IndexOf('=') + 1);
IEnumerable<char> stringQuery2 = input.TakeWhile(c => Char.IsDigit(c) || c=='.' || c=='-');
string result = string.Empty;
foreach (char c in stringQuery2)
result += c;
double dResult = double.Parse(result);
Try this
string s = "goal0=-1234.4334abc12423423";
string matches = Regex.Match(s, #"(?<=^goal\d+=)-?\d+(\.\d+)?").Value;
The regex says
(?<=^goal\d+=) - A positive look behind which means look back and make sure goal(1 or more number)= is at the start of the string, but dont make it part of the match
-? - A minus sign which is optional (the ? means 1 or more)
\d+ - One or more digits
(\.\d+)? - A decimal point followed by 1 or more digits which is optional
This will work if your string contains multiple decimal points as well as it will only take the first set of numbers after the first decimal point if there are any.
Use a regex for extracting:
x = Regex.Match(string, #"\d+").Value;
Now convert the resulting string to the number by using:
finalNumber = Int32.Parse(x);
Please try this:
string sample = "goal0=1234.4334abc12423423goal1=-234234asdfsdf";
Regex test = new Regex(#"(?<=\=)\-?\d*(\.\d*)?", RegexOptions.Singleline);
MatchCollection matchlist = test.Matches(sample);
string[] result = new string[matchlist.Count];
if (matchlist.Count > 0)
{
for (int i = 0; i < matchlist.Count; i++)
result[i] = matchlist[i].Value;
}
Hope it helps.
I didn't get the question at first. Sorry, but it works now.
I think this simple expression should work:
Regex.Match(string, #"\d+")
You can use the old VB Val() function from C#. That will extract a number from the front of a string, and it's already available in the framework:
result0 = Microsoft.VisualBasic.Conversion.Val(goal0);
result1 = Microsoft.VisualBasic.Conversion.Val(goal1);
string s = "1234.4334abc12423423";
var result = System.Text.RegularExpressions.Regex.Match(s, #"-?\d+");
List<String> list = new List<String>();
list.Add("goal0=1234.4334abc12423423");
list.Add("goal1=-23423");
list.Add("asdfsdf");
Regex regex = new Regex(#"^goal\d+=(?<GoalNumber>-?\d+\.?\d+)");
foreach (string s in list)
{
if(regex.IsMatch(s))
{
string numberPart = regex.Match(s).Groups["GoalNumber"];
// do something with numberPart
}
}
Given a string
var testData = "1234 test string 987 more test";
I want to be able to use a regex to pull out 1234 and 987. As far as I could tell using
var reg = new Regex(#"?<numbers>\d+");
should do what I want but when I say
var match = reg.match(testData);
I would think that
Assert.AreEqual(match.Groups["numbers"].Captures.Count(), 2);
but it's only 1. What am I doing wrong? Intuition tells me that the
?<group>
means there can only be 0 or 1 of these values. Should I not be using a named group?
*<group>
doesn't seem to work in the regex builder in visual studio but I did not try it in my tests.
Why didn't you use the pattern string as below:
Regex reg = new Regex(#"\d+");
and then get the numbers by:
MatchCollection matches = reg.Matches(testData);
After that, the matches variable contains 2 Match value which represent for 1234 and 987.
You also use the assert as:
Assert.AreEqual(matches.Count, 2);
Hope it will help you!
try {
Regex regexObj = new Regex(#"([\d]+)", RegexOptions.IgnoreCase);
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
for (int i = 1; i < matchResults.Groups.Count; i++) {
Group groupObj = matchResults.Groups[i];
if (groupObj.Success) {
// matched text: groupObj.Value
// match start: groupObj.Index
// match length: groupObj.Length
}
}
matchResults = matchResults.NextMatch();
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}