Jump to content
  • Advertisement
Sign in to follow this  
smitty1276

Regular expressions, lookaheads...

This topic is 5048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys... I'm trying to read as many integers as possible from a text file, without actually knowing the format of the file. I'm reading the whole file into a string, and then splitting the string around this pattern... "[^0-9]++". The problem is that I want to allow the minus sign to be pulled into the front of the string, but ONLY ONE OF THEM. Any other leading minus signs should be thrown onto the back end of the matched expression that splits the string. Am I making sense? "12blahblah ---200" should split into "1" and "-200". I'm not really proficient with reg expressions and was hoping someone might be able to explain how I might use the (?=X) or (?!X) operators there. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
char c = 0;
bool hasneg = false;
bool lastnum = false;
vector<int> ints;
int x = 0;
while(fread(&c,1,1,fin))
{
if(c == '-')
{
hasneg = true;
if(lastnum)
{
ints.pushback(hasneg ? -x : x);
x = 0;
lastnum = false;
}
}
else if((c>='0')&&(c<='9'))
{
x *= 10;
x += c-'0';
lastnum = true;
}
else
{
ints.pushback(hasneg ? -x : x);
x = 0;
hasneg = false;
lastnum = false;
}
}

Share this post


Link to post
Share on other sites
While this smells suspiciously like homework...

in perl [iirc]:


while (<>){
while($_=~/(-?\d+)/g){
print "$1\n";
}
}


This should extract and print each number [including negative numbers] on a line by themself. It will not detect commas [ie 12,843] or decimal numbers [ie $15.99].

Now. The actual regex is '-?\d+' which translates to "match one or none of "-" and then 1 or more digits. [\d means [0-9] in perl].

The "/g" modifier at the end is a perlism to keep the pointer in the string where the last match occured. That way it will get more than 1 number on a line rather than skipping at the first match.

Perl [and nearly every other regex flavor I know of] allows for the coder to extract the match the regex found. In perl, it places the () match in $1, which is printed as part of the loop.

Sorry if this wasn't clear, or helpful; it's late.

Share this post


Link to post
Share on other sites
It is homework, but the integer extracting part is not at all relevant to the actual assignment. I'm just doing this for "fun"... I thought it might help me become reacquainted with reg-exprs.

The problem with the regex you gave is that it specifies the expression that actually finds the integer itself. I'm using java's split method in the String class, which requires an expression to specify what falls BETWEEN the integers.

I can tell it to find any sequence of characters that are not digits and are not the '-' character, but I don't know how to tell it to allow the LAST '-' character before the integer. I am using Integer.parseInt(token) to convert these extracted strings to an integer, and it throws an exception if there is more than one - sign in front of the number. I was thinking that it would require a lookahead type deal where it knows to stop matching when only one '-' remains (if there are ANY to begin with).

Do you know of anything in J2SE that behaves like the perl you used? I don't know of any way to retrieve all substrings that match a pattern... only how to split a string around a matched pattern.

Share this post


Link to post
Share on other sites
I guess I'll have to play with the java.util.regex package. It just seems like there should be a way to extract the matches using the string class.

Oh, well.

Thanks for the help, though.

Share this post


Link to post
Share on other sites
Thanks, I'll play with that. Regular expressions are so incredibly powerful that its unfortunate that I personally almost never have to use them and therefore never had to become proficient with them. This sounds like a cool tool.

Thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!