I am new in Regex.
I want to splite my host URI like this:xxxPermanentWordChangeWord.myname.city.year.age
i wnat to get: xxx and myname with one pattern regex
i tried this
var pattern = " (?=(?<p0>(.*)PermanentWord))?(?=(?<p1>i dont know what to write here in order to get myname))?";
Thanks!
According you question and sample there is what you want.
The idea is:
Get the string before PermanentWordChangeWord..
The rule for this is (.*)PermanentWordChangeWord\.
Get the value after PermanentWordChangeWord. but before .(dot).
The rule for this is ([^\..]*) this mean match all but without .(dot)
var text = "xxxPermanentWordChangeWord.myname.city.year.age";
var pattern = #"(.*)PermanentWordChangeWord\.([^\..]*)";
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
Match match = reg.Match(text);
var output = string.Empty;
if (match.Success)
{
var group0 = match.Groups[0].Value; // This value is: xxxPermanentWordChangeWord.myname.city.year.age
var group1 = match.Groups[1].Value; //This value is: xxx
var group2 = match.Groups[2].Value; //This value is: myname
output = group1 + group2;
}
Related
Good Day,
I cant create the regex exact for my url:
any-any.test.test2.com or ALL CAPS
http://any-any.test.test2.com or ALL CAPS
https://any-any.test.test2.com or ALL CAPS
can someone please help me how can I make a regex that can accept this url's.
Useful Link:
https://regex101.com/
Note: any means user can input any characters and after first any should have "-" , then all must contain :
.test.test2.com or
.TEST.TEST2.COM
at the end.
Try this pattern:
var pattern = "(?i)(https?://)?.+-.+(.test.test2.com)"
Usage:
var test1 = "any-any.test.test2.com";
var test2 = "ANY-ANY.TEST.TEST2.COM";
var test3 = "http://any-any.test.test2.com";
var test4 = "https://any-any.test.test2.com";
var result1 = Regex.Match(test1, pattern);
var result2 = Regex.Match(test2, pattern);
var result3 = Regex.Match(test3, pattern);
var result4 = Regex.Match(test4, pattern);
Console.WriteLine(result1);
Console.WriteLine(result2);
Console.WriteLine(result3);
Console.WriteLine(result4);
Output:
any-any.test.test2.com
ANY-ANY.TEST.TEST2.COM
http://any-any.test.test2.com
https://any-any.test.test2.com
a simple regex pattern in vim
...-....test.test2.com\c
This will match
"any-any.test.test2.com";
"ANY-ANY.TEST.TEST2.COM"
Try this one:
(http(s)?://)?.+-.+.test.test2.com
There is a useful open source tool:
https://sourceforge.net/projects/regulator/
Match match = Regex.Match("https://any-any.test.test2.com", "(http(s)?://)?.+-.+\.test.test2.com", RegexOptions.IgnoreCase);
if (match.Success) {
Console.WriteLine("OK");
}
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'm trying to specifically get the string after charactername= and before " >. How would I use regex to allow me to catch only the player name?
This is what I have so far, and it's not working. Not working as it doesn't actually print anything. On the client.DownloadString it returns a string like this:
<a href="https://my.examplegame.com/charactername=Atro+Roter" >
So, I know it actually gets string, I'm just stuck on the regex.
using (var client = new WebClient())
{
//Example of what the string looks like on Console when I Console.WriteLine(html)
//<a href="https://my.examplegame.com/charactername=Atro+Roter" >
// I want the "Atro+Roter"
string html = client.DownloadString(worldDest + world + inOrderName);
string playerName = "https://my.examplegame.com/charactername=(.+?)\" >";
MatchCollection m1 = Regex.Matches(html, playerName);
foreach (Match m in m1)
{
Console.WriteLine(m.Groups[1].Value);
}
}
I'm trying to specifically get the string after charactername= and before " >.
So, you just need a lookbehind with lookahead and use LINQ to get all the match values into a list:
var input = "your input string";
var rx = new Regex(#"(?<=charactername=)[^""]+(?="")";
var res = rx.Matches(input).Cast<Match>().Select(p => p.Value).ToList();
The res variable should hold all your character names now.
I assume your issue is trying to parse the URL. Don't - use what .NET gives you:
var playerName = "https://my.examplegame.com/?charactername=NAME_HERE";
var uri = new Uri(playerName);
var queryString = HttpUtility.ParseQueryString(uri.Query);
Console.WriteLine("Name is: " + queryString["charactername"]);
This is much easier to read and no doubt more performant.
Working sample here: https://dotnetfiddle.net/iJlBKW
All forward slashes must be unescaped with back slashes like this \/
string input = #"<a href=""https://my.examplegame.com/charactername=Atro+Roter"" >";
string playerName = #"https:\/\/my.examplegame.com\/charactername=(.+?)""";
Match match = Regex.Match(input, playerName);
string result = match.Groups[1].Value;
Result = Atro+Roter
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
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
}