Welcome Guest ( Log In | Click here to Register a free account now! )
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.
Jun 24 2009, 08:09 AM
Post
#1
|
|
![]() I !<3 malware ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Senior Classmen Posts: 1,835 Joined: 8-June 08 From: Ontario Member No.: 214,918 |
I currently have the following code in VB .Net: CODE Dim matchcollection As System.Text.RegularExpressions.MatchCollection Dim currentmatch As System.Text.RegularExpressions.Match Dim lowercasetag As String matchcollection = Regex.Matches(lines, "\[(?i)(.+?)\]") For Each currentmatch In matchcollection lowercasetag = currentmatch.ToString.ToLower lines = Regex.Replace(lines, "\[(?i)(.+?)\]", "[" & lowercasetag & "]") Next The variable lines comes in as a string. The Regex is supposed to detect BBCode tags regardless of case (which is does perfectly) and then drop the case down to lower case. However, it seems that the lowercasetag variable cannot be understood by regex. I was wondering whether I was doing something wrong, or if there were an alternative way of doing this. Thanks again! This post has been edited by aommaster: Jun 24 2009, 08:10 AM -------------------- My website: http://www.aommaster.com
|
|
|
|
![]() |
Jun 24 2009, 11:40 AM
Post
#2
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 7,961 Joined: 5-June 04 From: Centerville, SD Member No.: 689 |
A regular expression is not a string.. it is a pattern.
http://www.astahost.com/info.php/Visual-Ba...ed33_t5171.html -------------------- |
|
|
|
Jun 24 2009, 11:55 AM
Post
#3
|
|
![]() I !<3 malware ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Senior Classmen Posts: 1,835 Joined: 8-June 08 From: Ontario Member No.: 214,918 |
Hi!
Thanks for your reply. I understand the regex works by using patterns, but I thought perhaps I could put in a pre-defined variable into it. Apparently not So my next question is, when my RegEx goes through the lines it reads, it correctly finds all the BBCode tags. How do I get it to change them to lowercase? I can't use the replace function because it is case sensitive. Is there something Regex can do to automatically change them to lower case? Thanks again! -------------------- My website: http://www.aommaster.com
|
|
|
|
Jun 24 2009, 12:11 PM
Post
#4
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 7,961 Joined: 5-June 04 From: Centerville, SD Member No.: 689 |
How about simply finding the bb tags, which are going to be the first and last characters in the string, and just make everything inside lower case? Again, I don't know the specific code for VB, but it would be something like:
newStr = str.substring(1, len(str)-1).toLower() The potential downside is that I don't know what will happen if the character is not a letter. In that case, you would have to loop through the string and check each character, and convert to lower case. The API for ToUpper and ToLower does not say what happens if the character is not a letter, so write a simple program that converts PL=#$ to lower case and see what happens. -------------------- |
|
|
|
Jun 24 2009, 12:27 PM
Post
#5
|
|
![]() I !<3 malware ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Senior Classmen Posts: 1,835 Joined: 8-June 08 From: Ontario Member No.: 214,918 |
Hi!
Regarding this: QUOTE How about simply finding the bb tags, which are going to be the first and last characters in the string, Not sure what you mean exactly by "frst and last characters in a string". I'll have to search for the opening square brackets for that to be the case. Consider bold text tags that lie in the middle of a sentance. The string doesn't start with the BBCode tag. I was looking for a way of pulling the value stored in $1, etc. from the regex, converting it to lowercase, and then throwing it back in. Your code here CODE newStr = str.substring(1, len(str)-1).toLower() VB .Net has similar syntax, so I can see what you're gettign at Maybe I misunderstood you, please correct me if I did Thanks again for your time! -------------------- My website: http://www.aommaster.com
|
|
|
|
Jun 24 2009, 12:29 PM
Post
#6
|
|
![]() Look buddy -- I'm an Engineer ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Coach Posts: 8,510 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
Hello
A better regex would be \[[^\]]+] it will execute faster. You can use the regex to find the bbtags and tolower to do the tolowering... something like this (where theString is your input) Dim theRegEx as System.Text.RegularExpressions.Regex("\[[^\]]+]") For Each theMatch as System.Text.RegularExpressions.Match in theRegEx.Match(theString) Dim lowerString as string = theString.Substring(theMatch.Index + 1, theMatch.Length - 2).ToLower(); theString.Remove(theMatch.Index + 1, theMatch.Length - 2) theString.Insert(theMatch.Index + 1, lowerString) End For Hope that helps, Billy3 EDIT: There are probably some syntax errors in there... it's been a while since I've written VB This post has been edited by Billy O'Neal: Jun 24 2009, 12:33 PM -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please consider a donation (by clicking this link). And that means I solve problems. Not problems like "What is beauty?" .. 'cause that would fall under the purview of your conundrums of philosophy.... |
|
|
|
Jun 24 2009, 12:39 PM
Post
#7
|
|
![]() Look buddy -- I'm an Engineer ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Coach Posts: 8,510 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
The potential downside is that I don't know what will happen if the character is not a letter. In that case, you would have to loop through the string and check each character, and convert to lower case. The API for ToUpper and ToLower does not say what happens if the character is not a letter, so write a simple program that converts PL=#$ to lower case and see what happens. It leaves characters for which there is no lowercase representation unchanged. Billy3 -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please consider a donation (by clicking this link). And that means I solve problems. Not problems like "What is beauty?" .. 'cause that would fall under the purview of your conundrums of philosophy.... |
|
|
|
Jun 24 2009, 01:11 PM
Post
#8
|
|
![]() I !<3 malware ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Senior Classmen Posts: 1,835 Joined: 8-June 08 From: Ontario Member No.: 214,918 |
Hi Billy!
This was the code I ended up using: CODE Dim theRegEx As New System.Text.RegularExpressions.Regex("\[[^\]]+]", System.Text.RegularExpressions.RegexOptions.IgnoreCase) For Each theMatch As System.Text.RegularExpressions.Match In theRegEx.Matches(lines) Dim lowerString As String = lines.Substring(theMatch.Index + 1, theMatch.Length - 2).ToLower() lines = lines.Remove(theMatch.Index + 1, theMatch.Length - 2) lines = lines.Insert(theMatch.Index + 1, lowerString) Next Needless to say, you got it pretty close Thanks again Billy! -------------------- My website: http://www.aommaster.com
|
|
|
|
Jun 24 2009, 01:23 PM
Post
#9
|
|
![]() Look buddy -- I'm an Engineer ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Coach Posts: 8,510 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
You're welcome
You can remove the "System.Text.RegularExpressions.RegexOptions.IgnoreCase" part... [ and ] characters are the same upper and lower case ;) Billy3 -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please consider a donation (by clicking this link). And that means I solve problems. Not problems like "What is beauty?" .. 'cause that would fall under the purview of your conundrums of philosophy.... |
|
|
|
Jun 24 2009, 01:27 PM
Post
#10
|
|
![]() I !<3 malware ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Senior Classmen Posts: 1,835 Joined: 8-June 08 From: Ontario Member No.: 214,918 |
Ahh right, you are! I was trying to find out why VB net just yelled at me so badly, so I threw that in just to make sure case sensitivity wasn't a problem
Thanks again! -------------------- My website: http://www.aommaster.com
|
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 21st November 2009 - 10:40 PM |