In my C# Console App I'm trying to use Regex to search a string to determine if there is a match or not. Below is my code but it is not quite working right so I will explain further. sSearchString is set to "_One-Call_Pipeline_Locations" and pDS.Name is a filename it is searching against. Using the code below it is set to true for Nevada_One-Call_Pipeline_Locations and Nevada_One-Call_Pipeline_LocationsMAXIMUM. There should be a match for Nevada_One-Call_Pipeline_Locations But Not for Nevada_One-Call_Pipeline_LocationsMAXIMUM. How can I change my code to do this properly?
Thanks in advance
if (Regex.IsMatch(pDS.Name, sSearchString))
Change the sSearchString to ".*_One-Call_Pipeline_Locations$"
You need to specify that a matching name must end with the text you have entered using the dollar token.
sSearchString = "_One-Call_Pipeline_Locations$";
Since you provided no details as to what else should match, we can only assume that if the string ends with "Nevada_One-Call_Pipeline_Locations", then it matches? Is this correct?
If so, you don't need Regex:
if (pDS.Name.EndsWith("Nevada_One-Call_Pipeline_Locations"))
{ //...
Related
I need to check the syntax of a file name in order to treat it and exclude files that don't match the expression.
The file names must be as follows:
(2 caracters letter or number)_(some caracters letters and numbers)__(YYYY-MM-dd-HH-SS-MM).csv
The last part is a date
In the middle we have two underscores
Can you help me for this request ? I'm not familiar at all with the regex and the few tests I made were not good.
Thanks a lot :)
I have created some sample. please find below it will work for you also you can modify as per your requirement.
For example : (22)-(ww)-(aa).csv
^[(\[][a-zA-Z0-9]{2}[)\]]+(-[(\[][a-zA-Z0-9]{2}[)\]]+)+(-[(\[][a-zA-Z0-9]{2}[)\]]+.csv)$
For example : (22)_(ww)_(01-12-2018 19:20).csv
^[(\[][a-zA-Z0-9]{2}[)\]]+(_[(\[][a-zA-Z0-9]{2}[)\]]+)+(_[(\[]([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\d\d\d\d [012]{0,1}[0-9]:[0-6][0-9][)\]]+.csv)$
For example: (22)_(ww)_(1999-12-31-23-59-59).csv
^[(\[][a-zA-Z0-9]{2}[)\]]+(_[(\[][a-zA-Z0-9]{2}[)\]]+)+(_[(\[]19\d{2}(-|\/)((0[1-9])|(1[0-2]))(-|\/)((0[1-9])|([1-2][0-9])|(3[0-1]))(-)(([0-1][0-9])|(2[0-3]))-([0-5][0-9])-([0-5][0-9])[)\]]+.csv)$
I recommend you to refer this URL Click Here it will be helpful for learning as well as you can find more example. Thanks
This is the RegEx suited to your request if the String contains only the filename
^.{2}_.+__\d{4}(?:-\d{2}){5}.csv$
And this if you want to capture it from a longer String for example
(.{2}_.+__\d{4}(?:-\d{2}){5}.csv)
If you are having difficulties to create a RegEx i can recommend you to take a look at RegExr.
I got strings which i want to take only those of them which have ending on for instance:
-id-3 (e.g somother33-id-3)
-id-203 (e.g som78estringetc-id-203)
-id-54 (e.g fwefwefwefw-id-3)
but sometimes i am retreiving strings which looks like this one i dont want to get
som78estringetc-id-203:someotherstring3-1
i am intrested only those string which ends by -id-somedigit
So string which i would like to get are those ending by convention:
somestring-id-digit
Could anyone help me how can i achieve that?
Using regex you can simply do:
-id-[0-9]*$
If you want to exclude your other item you could try:
[a-zA-Z0-9]*-id-[0-9]*$
try to use regex.
Regex.IsMatch("asdfadid-13234234", #"\w*-Id-[0-9]*$\b", RegexOptions.IgnoreCase)
this case best would be #"\w*-Id-[0-9]*$\b" right?
I want to validate a URL using regular expression. Following are my conditions to validate the URL:
Scheme is optional
Subdomains should be allowed
Port number should be allowed
Path should be allowed.
I was trying the following pattern:
((http|https)://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
But I am not getting the desired results. Even an invalid URL like '*.example.com' is getting matched.
What is wrong with it?
are you matching the entire string? you don't say what language you are using, but in python it looks like you may be using search instead of match.
one way to fix this is to start you regexp with ^ and end it with $.
While parsing URL's is best left to a library (since I know perl best, I would suggest something like http://search.cpan.org/dist/URI/), if you want some help debugging that statement, it might be best to try it in a debugger, something like: http://www.debuggex.com/.
I think one of the main reasons it is matching, is because you don't use beginning and ending string match markers. Meaning, no part of that string might be matching what you put in explicitly, but because you haven't marked it with beginning and end markers for the string, your regex could just be matching 'example.com' in your string, not the entire input.
Found the regular expression for my condition with help from your inputs
^(http(s)?://)?[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$
Following code works for me in c#
private static bool IsValidUrl(string url)
{
return new Regex(#"^(http|http(s)?://)?([\w-]+\.)+[\w-]+[.\w]+(\[\?%&=]*)?").IsMatch(url) &&!new Regex(#"[^a-zA-Z0-9]+$").IsMatch(url);
}
it allows "something.anything (at least 2 later after period) with or without http(s) and www.
Hello I have a question about matching groups based on the following regular expression
static string partPattern = #"^(?<Key>\w+)\s*(?<Value>.*)$";
Sample Data as following:
TEST_REPLICATE
{
REPLICATE_ID 1986
ASSAY_NUMBER 877
ASSAY_VERSION 4
ASSAY_STATUS "Research"
}
I am able to retrieve values correctly and if values are NULL, it works correctly as well. What I am trying to do is to also retrieve a value for instance the last one module which is in double quotes. I am not really sure if i am doing it correctly, would this be the correct regex for the above scenario, I just added quotes before w. Please correct, thanks
static string partPattern = #"^(?<Key>\"w+)\s*(?<Value>.*)$";
You regex is not correct.Atleast for the input you have provided..
If I have understood your question,this is the regex that you need.
^\s*(?<Key>\w+)\s*\"?(?<Value>.*?)\"?$
It would work with multiline mode...
Not sure where your problem is. This works for me:
\s*(?<Key>[^\s]+)\s*(?<Value>.*)
I am new to regular expressions and the one that i have written might be a very simple one but donot know where I am wrong.
#"^([a-zA-Z._]+)#([\d]+)"
This RE is for the following string:
somename#somenumber
Now i am trying to retrieve the somename and somenumber. This is what i did:
ac.name = m.Groups[0].Value;
ac.number = m.Groups[1].Value;
Here ac.name reads the complete string, and ac.number reads somenumber. Where am I wrong in ac.name?
i guess the regex is correct, the problem is, you get the ac.name not from group 1 but group(0), which is the whole string. try this:
ac.name = m.Groups[1].Value;
ac.number = m.Groups[2].Value;
This regex is correct. I think your mistake is in somewhere else. You seem to use C#. So, you should think about the regex usage in the language.
Looking to the code sample in MSDN, you need to use 1-based indexes while accessing Groups instead of zero-based (as also Kent suggested). So, use this:
String name = m.Groups[1].Value;
String number = m.Groups[2].Value;
use this regex (\w+)#(\d+([.,]\d+)?)
Groups[1] will be contain name
Groups[2] will be contain number
I think you should move the + into the capture group:
#"^([a-zA-Z._]+)#([\d]+)"
If this is C#, try without the ^
([a-zA-Z\._]+)#([\d]+)
I just tried it out and it groups properly
Update: escaped the .
If you want only one match (and hence the ^ in original expression), use .Match instead of .Matches method. See MSDN documentation on Regular Expression Classes.