Help - Search - Members - Calendar
Full Version: Regex Help
BleepingComputer.com > Software > Programming
   
aommaster
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!
groovicus
A regular expression is not a string.. it is a pattern.

http://www.astahost.com/info.php/Visual-Ba...ed33_t5171.html
aommaster
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!
groovicus
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.
aommaster
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!
Billy O'Neal
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
Billy O'Neal
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
aommaster
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!
Billy O'Neal
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
aommaster
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!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.