Remove last four characters in string that's part of url? - c#

Here is my code:
Search Related
I want to take the text value of lblGraphicNameValue and remove the last four characters. Can I do this, and keep it inline? Or is this something I should do in the code behind?
Thanks!

I think this would work:
Search Related
or also this:
Search Related
I'm not sure, I have no experience with ASP.NET though so I don't know if it allows arbitrary code.

You can also use the Remove method:
lblGraphicNameValue.Text.Remove(lblGraphicNameValue.Text.Length - 4)

Related

Stop regex from spanning across unrequired content

I need to extract a series of meaningful values from a file. The basic pattern for the values I need to match looks like:
"indicator\..+?"\[true\]
Unfortunately, in places this is spanning across quite a bit of content to get a true match, and the lazy quantifier (?) is not being as lazy as I'd like.
How do I modify the above so that out of the following:
"indicator.value here"[false],"other content","more other
content","indicator don't match this one because the full stop is missing"[true],"indicator.this is the
value I want matched"[true]
only this value is returned: "indicator.this is the value I want matched"[true]
Currently, that whole string is being returned by my above regex.
Assuming commas are the delimiter - simply avoid matching on them:
#"""indicator\.[^,]+?""\[true\]"
Try using "indicator\.(.*)?"\[true\] instead and see if that helps. I think the lazy only applies to the * operator. I vaguely remember having this issue years ago.
You can leverage the discard technique by discarding the pattern you don't want. So, you could have something like this:
"indicator\..+?"\[false\]|"indicator\.(.+?)"\[true\]
Discard this pattern --^ Capture this --^
Working demo
Match information
MATCH 1
1. [150-182] `this is the value I want matched`

Filter string based on ending convention

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?

How to display a Superscript in C# Dropdownlist, using html tags

Hope someone can tell me how I should get a Superscript tag like ² to display correctly in the text of a Dropdownlist option?
Thanks.
Probably using HTML entities:
²
instead of the actual character. But it's probably better to let C# take care of it:
string safeString = HttpUtility.HtmlEncode("your string²");
// Use the result as the displayed value in your Dropdownlist
This method will also find other problematic characters such as & and replace them accordingly. See MSDN HttpUtility.HtmlEncode for more information on this.
Edit: be advised; the resulting string from HtmlEncode will show (when used in HTML) exactly that what you have input in the method. So do not use HTML entities in your input, because then that's exactly what you'll see in the resulting page.
If you want to show m² then just enter that inside the method. .NET will take care of the rest.
Maybe unicode symbols would do the trick for you: http://tlt.its.psu.edu/suggestions/international/bylanguage/mathchart.html#super
For the superscripted two you would use ² resulting in: ²
You can write this way,
string item=HttpUtility.HtmlDecode("ml/min/1.73m²")
for more info on superscript you can see this link
http://symbolcodes.tlt.psu.edu/bylanguage/mathchart.html#super

Regex Help (again)

I don't really know what to entitle this, but I need some help with regular expressions. Firstly, I want to clarify that I'm not trying to match HTML or XML, although it may look like it, it's not. The things below are part of a file format I use for a program I made to specify which details should be exported in that program. There is no hierarchy involved, just that each new line contains a 'tag':
<n>
This is matched with my program to find an enumeration, which tells my program to export the name value, anyway, I also have tags like this:
<adr:home>
This specifies the home address. I use the following regex:
<((?'TAG'.*):(?'SUBTAG'.*)?)?(\s+((\w+)=('|"")?(?'VALUE'.*[^'])('|"")?)?)?>
The problem is that the regex will split the adr:home tag fine, but fail to find the n tag because it lacks a colon, but when I add a ? or a *, it then doesn't split the adr:home and similar tags. Can anyone help? I'm sure it's only simple, it's just this is my first time at creating a regular expression. I'm working in C#, by the way.
Will this help
<((?'TAG'.*?)(?::(?'SUBTAG'.*))?)?(\s+((\w+)=('|"")?(?'VALUE'.*[^'])('|"")?)?)?>
I've wrapped the : capture into a non capturing group round subtag and made the tag capture non greedy
Not entirely sure what your aim is but try this:
(?><)(?'TAG'[^:\s>]*)(:(?'SUBTAG'[^\s>:]*))?(\s\w+=['"](?'VALUE'[^'"]*)['"])?(?>>)
I find this site extremely useful for testing C# regex expressions.
What if you put the colon as part of the second tag?
<((?'TAG'.*)(?':SUBTAG'.*)?)?(\s+((\w+)=('|"")?(?'VALUE'.*[^'])('|"")?)?)?>

Removing <div>'s from text file?

Ive made a small program in C#.net which doesnt really serve much of a purpose, its tells you the chance of your DOOM based on todays news lol. It takes an RSS on load from the BBC website and will then look for key words which either increment of decrease the percentage chance of DOOM.
Crazy little project which maybe one day the classes will come uin handy to use again for something more important.
I recieve the RSS in an xml format but it contains alot of div tags and formatting characters which i dont really want to be in the database of keywords,
What is the best way of removing these unwanted characters and div's?
Thanks,
Ash
If you want to remove the DIV tags WITH content as well:
string start = "<div>";
string end = "</div>";
string txt = Regex.Replace(htmlString, Regex.Escape(start) + "(?<data>[^" + Regex.Escape(end) + "]*)" + Regex.Escape(end), string.Empty);
Input: <xml><div>junk</div>XXX<div>junk2</div></xml>
Output: <xml>XXX</xml>
IMHO the easiest way is to use regular expressions. Something like:
string txt = Regex.Replace(htmlString, #"<(.|\n)*?>", string.Empty);
Depending on which tags and characters you want to remove you will modify the regex, of course. You will find a lot of material on this and other methods if you do a web search for 'strip html C#'.
SO question Render or convert Html to ‘formatted’ Text (.NET) might help you, too.
Stripping HTML tags from a given string is a common requirement and you can probably find many resources online that do it for you.
The accepted method, however, is to use a Regular expression based Search and Replace. This article provides a good sample along with benchmarks. Another point worth mentioning is that you would require separate Regex based lookups for the different kinds of unwanted characters you are seeing. (Perhaps showing us an example of the HTML you receive would help)
Note that your requirements may vary based on which tags you want to remove. In your question, you only mention DIV tags. If that is the only tag you need to replace, a simple string search and replace should suffice.
A regular expression such as this:
<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>
Would highlight all HTML tags.
Use this to remove them form your data.

Categories

Resources