Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have following text with me,
File <a class='ub link' data-menu-identifier='files' data-url-identifier='/files/detail/n0tlz'>Medical bills & LTA.xlsx</a> was attached to the case by {updatedby} on {updatedon}
What I need is, I need regex that will give me 'n0tlz' from the given text. Please take note that this is not static I need to extract from this pattern.
I have achieved at some level but not getting how to get required text;
I have : '/files/detail/n0tlz' where '/files/detail/{code}' is always there, I just want value of {code} in given sample {code} = n0tlz
// Extract The filecode
const string pattern = #"files\/detail\/(.*)'>";
// Instantiate the regular expression object.
var r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
var m = r.Match(fileUpdate.UpdateText);
here m has value : /files/detail/n0tlz'>
but I just wants 'n0tlz' I feels I am too close, just one last step is requires to obtain code value.
// Extract The filecode
const string pattern = #"files\/detail\/(.*)'>";
// Instantiate the regular expression object.
var r = new Regex(pattern, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
var m = r.Match(fileUpdate.UpdateText);
if (m.Success && m.Groups.Count == 2 )
{
m.Groups[1].Value; // This is the expected result
}
If data-url-identifier='/files/detail/n0tlz'> always contains a '> you could use that as a delimiter. making your existing
files\/detail\/ into files\/detail\/ '>
now to capture the specific value you can use a simple capture group
(.*) which will catch any character between zero and unlimited times.
Combining the 2 would lead to files\/detail\/(.*)'>.
Edit
But this m have : /files/detail/n0tlz'> I just want 'n0tlz' can i get it?
Well, yes. Or to be even more detailed, you already have it!
Instead of using a var try defining the type itself. In this case that would be a Match. Taking a look at the properties you will see that it contains a lot more then just the whole match.
Groups will contain your value
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have shared the relevant image below.
Text in image ;
"C:\Users\Hp\OneDrive-blablablabla\Masaustu\EdaGorsel\4.PNG\tmanager\tmanager\t2022/12/12\r\nddd\tV1.0"
I only want 4.PNG from the above post
2.I should not use special names or filenames
3.Ex: \t between \ like this
I hope I was able to explain my problem
you could use a Regex match to get this part of the url :
private Regex _regex = new Regex(#"^.*\\(?<Image>.*\.PNG)\\.*$");
(You can test regex here)
Then use this to read the match :
var result = _regex.Match(url).Groups["Image"].Value;
Another, simplier solution, would be to use :
url.Split("\\")[6];
This works only if the searched part is always at the same spot.
You can split your strings into many shorter ones using .Split function:
string[] words = path.Split("\\");
Using the double \ to escape the character..
If you don't guarantee that the string you need is always at the same position, you can use this to fetch it from the list:
string neededString = words .FirstOrDefault(str => str.Contains(".PNG"));
This will return the first instance of any word from your string that contains ".png" in it.
there is sevral ways
if you know number of "" in your string then split it(str.Split(' \ ')) and get wanted index
use regular expersion to extract filename
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have the below string (which was originally XML):
<Message><ProcedureName>TestUpdate</ProcedureName><Parameters><![CDATA[N'Yes',N'No']]></Parameters><MessageType>A</MessageType></Message>
I have tried the below but the text 'No' is dynamic (it can be a different value):
message.Replace("No", "********");
I'm not sure whether I can do this using string.Replace, Regex or a mix of both?
You can use regex to get string between 2 tags before replacing:
string str = "<Message><ProcedureName>TestUpdate</ProcedureName><Parameters><![CDATA[N'Yes',N'No']]></Parameters><MessageType>A</MessageType></Message>";
Match match = Regex.Match(str, #"CDATA\[.*\]");
if (match.Success)
{
Console.WriteLine(str_out);//"CDATA[N'Yes', N'No']]"
}
Assuming by 'dynamic' you mean it could be anything, including 'No', here's my attempt:
var source = "<Message><ProcedureName>TestUpdate</ProcedureName><Parameters><![CDATA[N'Yes',N'No']]></Parameters><MessageType>A</MessageType></Message>";
var regexstr = #"(.*\[CDATA\[.*,N')(.*)('\]\].*)";
var regex = new Regex(regexstr);
var matches = regex.Match(source);
Console.WriteLine($"{matches.Groups[1].Value}****{matches.Groups[3].Value}");
//Console Output: <Message><ProcedureName>TestUpdate</ProcedureName><Parameters><![CDATA[N'Yes',N'****']]></Parameters><MessageType>A</MessageType></Message>
This could possibly be optimised further but that is dependent on your source. Hope it helps!
This is what I have settled on. It is not perfect but it satisfies my requirement.
Match match = Regex.Match(message, #"CDATA\[.*\]");
if (match.Success)
{
maskedMessage = message.Replace(match.Value, "********");
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a string like this:
string str = "Property first {prop1=val1;prop2=val2}this is reg table[uvm]dsfhsjhsdj[/uvm]this is uvm test{pp1=vv2}";
And I need to get [uvm]...[/uvm] string.
I have tried this:
reg = new Regex(#"(\[uvm\].*?\[\\uvm\])");
string s = reg.Match(str).Groups[0].Value;
But it is not working. It does not match. What should I do?
The problem is not the Regular Expression, but rather the string input.
You input contains [/uvm] and not [\uvm] which of course won't be matched by the Regular Expression.
Also, I would suggest you to move the Capture to catch the inside of uvm, by simply moving the parenthesis inside the regex, like this:
\[uvm\](.*?)\[\\uvm\]
The regex you're using is a little off - you're using the wrong type of slash, try this:
var reg = new Regex(#"(\[uvm\].*?\[\/uvm\])");
Also note that if you want to retrieve the text within the [uvm] tags, you would need an additional group. Here's a working example:
var str = "Property first {prop1=val1;prop2=val2}this is reg table[uvm]dsfhsjhsdj[/uvm]this is uvm test{pp1=vv2}";
var reg = new Regex(#"(\[uvm\](.*?)\[\/uvm\])");
var m1 = reg.Match(str).Groups[1].Value; // = "[uvm]dsfhsjhsdj[/uvm]"
var m2 = reg.Match(str).Groups[2].Value; // = "dsfhsjhsdj"
Working example
The accessing of the groups would obviously need to be much more robust - this is purely for demonstrative purposes.
You do not need to set any capturing groups and get your value using Regex.Match(str).Value if you use look-arounds:
string str = "Property first {prop1=val1;prop2=val2}this is reg table[uvm]dsfhsjhsdj[/uvm]this is uvm test{pp1=vv2}";
Regex rx = new Regex(#"(?<=\[uvm]).*?(?=\[/uvm])");
foreach (Match m in rx.Matches(str))
{
Console.WriteLine(m.Value);
}
Or, you can get all matches using LINQ:
var matches = rx.Matches(str).Cast<Match>().Select(p => p.Value).ToList();
See IDEONE demo
REGEX EXPLANATION:
(?<=\[uvm]) - A positive look-behind making sure there is literal [uvm] string before our expected result
.*? - Any string of characters (except newline, if you need . to match a newline, add RegexOptions.Singleline flag in the Regex declaration) as few as possible (due to *? lazy quantifier)
(?=\[/uvm]) - A positive look-ahead that makes sure there is literal [/uvm] after our expected result.
See regex demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i need filter a string with a regular expresion. I have read that I must use the method Replace of the class Regex with a pattern but I don't know that Expresion Regular I need to filter my String.
I have a large string s and I want extract or filter the ? in the next pattern of the string:
"string fixed?_IP"
string fixed is a string that always is the same
IP is a valid IP number
Please I need the pattern to pass to the function Replace of the class Regex to obtain the string ?
I try some patterns but anyone works.
pattern1="(ftp_files\\?_[0-9]+.[0-9]+.[0-9]+.[0-9]+)"
pattern2= "(ftp_files\\?_/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/)"
ftp_files\ is the string fixed
In pattern2 I try an expresion regular that I have found to validate an IP.
You don't need to call Regex.Replace if what you want is to extract the information. You have to call Regex.Match for that. Or, if you really want to call Replace, please tell us what part you want to replace, and what should the substitute look like.
var match = Regex.Match(inputString, #"ftp_files\\\?_(?<ip>\d+\.\d+\.\d+\.\d+)");
var ipAddress = match.Groups["ip"].Value;
Here I chose a more legible regex for the ip address (\d+\.\d+\.\d+\.\d+). If you really need to ensure each byte of the IP is valid, you could replace each instance of \d+ in that expression with something like this: (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).
EDIT: If you want to extract what's between ftp_files\ and _IP, here's the regex:
var match = Regex.Match(inputString, #"ftp_files\\(?<value>.*?)_(?<ip>\d+\.\d+\.\d+\.\d+)");
var value = match.Groups["value"].Value;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
So I am trying to replace the characters below from a users input which relates to a SQL column in a table.
Example:
User creates a field called My(first)try. In SQL this comes up as Myfirsttry.
I am currently write code to replace the users input with our and I am stuck.
I have this so far.
itemreplace = itemreplace.Replace("(", "");
This however, doesn't do the trick. Thoughts, comments, suggestions?
I feel like the real-world case is more complicated, and you're not indicating that, but to handle your example text My(first)try you could just chain the Replace statements:
itemreplace = itemreplace.Replace("(", "").Replace(")", "");
However, it seems like the real-world case is more along the lines of leveraging a Regex like this:
^[a-zA-Z0-9_##][a-zA-Z0-9#$#_]*$
and here is a Regex 101 that would prove that.
Then using that might look like this:
var valid = Regex.IsMatch("My(first)try", pattern);
I did reference this post, What special characters are allowed in T-SQL column name?, to determine the allowed characters for a column name.
First option:
String itemreplace = new String("My(first)try");
String charsToRemove = new String[] {"(", ")"};
foreach (char c in charsToRemove)
{
itemreplace = itemreplace.Replace(c, string.Empty);
}
Second option:
itemreplace = itemreplace.Replace("(", string.Empty).Replace(")", string.Empty);