Regex: question about ?= and any characters [duplicate] - c#

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I have a target string like:
"addr: line1
line2
tel:12345678"
note: between line2 and tel, there might be 1 or multiple new lines:\r\n or \r\n\r\n or more.
The result I want to get is as below:
"addr: line1
line2"
no \r\n under line2.
My questions are:
1)If I use
/addr[\s\S]+(?=(\r\n)+tel)/
, i will get the addr without tel, but I can't get rid of "\r\n"s under "line2", how could I do that?
2)I know [\s\S] represents any characters including \r,\n, and (.|\n|\r) can do that too.
But why [.\n] can't? It's just like the syntax of[\s\S] isn't it?
Thank you very much!

You need to make the first "+" non-greedy, so that it does not match the whole whitespace before "tel"
var regex = new Regex(#"addr.+?(?=\s+tel)", RegexOptions.Singleline);
var result = regex.Match(text).Value;

Related

Check if a string only contains the characters > or < or - [duplicate]

This question already has answers here:
.NET Regex Error: [x-y] range in reverse order
(3 answers)
How to match hyphens with Regular Expression?
(6 answers)
Closed 4 years ago.
What I need is to check if a string only contains the characters > or < or -.
So I thought using a RegEx for this, and I found an SO question with the exact same problem, and it has an answer (not the accepted one but the answer with regex)
This is the SO question : String contains only a given set of characters
So I modified the expression in this question to fit my needs like this :
static readonly Regex Validator = new Regex(#"^[><- ]+$");
and I call it like this ;
Validator.IsMatch(testValue)
But it's throwing the error
x-y range in reverse order
There are lots of question on SO about this error but I cant find or understand the answer I need.
So what am I doing wrong with this RegEx?
^[-<>]+$ "-" must come first in C# regex
Escape - within character groups. ([0-9] means "zero to nine" and not "zero, dash or nine")

Regex Pattern to check only letters [duplicate]

This question already has answers here:
C# Regex to allow only alpha numeric
(6 answers)
Closed 5 years ago.
I need to check whether string is only contain letters but not numbers or special characters.I used below regex pattern,
String validText = "^[a-zA-Z-]+$";
its work fine for 'Leo#' but if it is like 'Leo#1' its not working properly.
Anyone have idea ?
I prefer you can use LinQ (input is your test string)
bool result = input.All(Char.IsLetter);
Else as Gordon Posted the right Regex,
^[a-zA-z]+$
You can try using this regex
/^[A-Za-z]+$/
This will match only letters in your string ..

What regex expression would be appropriate? [duplicate]

This question already has answers here:
How to use string.Endswith to test for multiple endings?
(9 answers)
Closed 5 years ago.
I need to check if a string last word is either "...abc" or "...xyz" or "...fgh".
How i can achieve the same thing using regex as i am trying to learn it?
e.g Sentence 1: Hi My Name is abc.
Sentence 2: I live in xyz.
The above sentence is a sample one to demonstrate.
You don't need any Regex. Just use String.EndsWith :
string a = "asdasd abc";
Console.WriteLine(a.EndsWith("abc.") || a.EndsWith("xyz.") || a.EndsWith("fgh."));
You can use this simple regex pattern:
(abc|xyz|fgh)$
Put your possible options between parenthesis separated by pipes. The $ means the end of the string.

Need to get so substring but using Regular Expression [duplicate]

This question already has answers here:
C# Substring Alternative - Return rest of line within string after character
(5 answers)
Closed 6 years ago.
I got on url like.
http://EddyFox.com/x/xynua
Need to fetch substring after /x/ what ever string is there.
complex example I faced is :
http://EddyFox.com/x//x/
Here result should be /x/
It can be achieved with substring ,But we need to perform it with regular expression.
This should do it:
string s = "http://EddyFox.com/x/xynua";
// I guess you don't want the /x/ in your match ?=!
Console.WriteLine(Regex.Match(s, "/x/(.*)").Groups[1].Value );
this is probably even better:
Console.WriteLine(Regex.Match(s, "(?<=/x/)(.*)").Value );
the output is
xynua
Have a look at this post: Regex to match after specific characters SO is full of RegEx posts. The probability is very high that a RegEx question has already been asked before. :)
The regex /x/(.*) will capture everything following the /x/
And where is the problem?
var r = new Regex("/x/(\\S*)");
var matches = r.Matches(myUrl);
This regex matches everything from /x/ until the first occurence of a white-space.

How to get the value of a regex capture group? [duplicate]

This question already has answers here:
How to read RegEx Captures in C#
(3 answers)
Closed 7 years ago.
First of all sorry about the tittle. Didn't know how to explain it.
This is my code:
string sString = #"docs/horaires/1/images/1"
var PickImage = Regex.Matches(sString, "/horaires/(.*?)/images/");
Console.WriteLine(PickImage[0].Value);
This will print /horaires/1/images/ instead of 1. I tried all the RegexOptions but didn't find the solution in there.
What am I doing wrong here?
This is what you need:
string sString = #"docs/horaires/1/images/1";
var pickImage = Regex.Match(sString, #"/horaires/(.*?)/images/");
if (pickImage.Success)
Console.WriteLine(pickImage.Groups[1].Value);
In your original code, PickImage[0] is a Match object, and Value will return the full match. You want the first captured group, so use match.Groups[1].Value. Note that Groups[0] always contains the full match.
No need to use Matches is you want a single result, use Match instead.

Categories

Resources