Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Virus, Spyware, and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
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.
Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

> Regex Help
aommaster
post 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



Hi guys!

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


--------------------
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies (1 - 9)
groovicus
post 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


--------------------
Never Argue With Stupid People



Microsoft Senior Student Partner
Go to the top of the page
 
+Quote Post
aommaster
post 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 sad.gif

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!


--------------------
Go to the top of the page
 
+Quote Post
groovicus
post 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.


--------------------
Never Argue With Stupid People



Microsoft Senior Student Partner
Go to the top of the page
 
+Quote Post
aommaster
post 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 smile.gif Here, I'll need to take some care, I think, in definig what the string "str" is. It can't be the whole line, it will need to be the BBCode line. But then the question comes up as to how to get those lines out, and again, same problem as above, how do I bring it down to lowercase?

Maybe I misunderstood you, please correct me if I did smile.gif

Thanks again for your time!


--------------------
Go to the top of the page
 
+Quote Post
Billy O'Neal
post 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 smile.gif

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


--------------------
Go to the top of the page
 
+Quote Post
Billy O'Neal
post 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



QUOTE(groovicus @ Jun 24 2009, 01:11 PM) *
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


--------------------
Go to the top of the page
 
+Quote Post
aommaster
post 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 tongue.gif

Thanks again Billy!


--------------------
Go to the top of the page
 
+Quote Post
Billy O'Neal
post 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 smile.gif

You can remove the "System.Text.RegularExpressions.RegexOptions.IgnoreCase" part... [ and ] characters are the same upper and lower case ;)

Billy3


--------------------
Go to the top of the page
 
+Quote Post
aommaster
post 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 smile.gif

Thanks again!


--------------------
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 21st November 2009 - 10:40 PM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Virus Removal Guides

© 2003-2009 All Rights Reserved Bleeping Computer LLC.