Jump to content

StringRegExp is kicking my butt. HELP!


aslani
 Share

Recommended Posts

Ok here's the situation. Say I have a sentence that is 200 characters long. I need StringRegExp to identify every words and spaces then array it. The reason for this is that the sentence has to be divided since the database provided can only accomodate 35 characters per line, then AutoIT have to put the rest of the sentence to the next line, then into the next line if applicable, etc.

That 200 characters sentence could be of any combination of words and can also be more or less than 200 characters.

Here's an example sentence;

Parts needs to be replaced per customer request.

The 35th character is "t" in "customer". My script currenly breaks that word.

Line 1: Parts needs to be replaced per cust

Line 2: omer request.

Then I remember that StringRegExp can solve this problem, but I'm so confused that I don't even know where to start.

I want it to come out this way;

Line 1: Parts needs to be replaced per

Line 2: customer request.

Even though the first line is not 35 characters long, it didn't break the word.

Please help.

Thanks.

Edited by aslani

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

if you always want to break the line after several chars u can use:

$line1 = Stringleft(Parts needs to be replaced per customer request, 30)

$line2 = Stringtrimleft(Parts needs to be replaced per customer request, 31) ; + 1 to remove space.

if i haven't understood what u need... ask again :P

EDIT:

i've read some more and here is a better one for u...

$str = @YOUR LINE THAT U NEED TO PARSE !

While stringlen($str)>35

$cut = Stringleft($str, 35)

$str = StringTrimLeft($str, 35)

iniwrite(..,...,...,...,$Cut)

Wend

this should cut the string and save it to an ini in cuts of 35...

Edited by Armand

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Link to comment
Share on other sites

  • Moderators

StringRegExp($sString, "(?s).{0,35}", 3)

Edit:

Forgot to make the exception in case the last couple of whatever is less than 35 chars.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Closest i could get... I can't figure out how to get it to match the punctuation at the end though.

#include <Array.au3>
$String = "Parts needs to be replaced per customer request."

$Result = StringRegExp($String, ".{0,35}\b\.?", 3);edit for punctuation

_ArrayDisplay($Result)
Edited by Paulie
Link to comment
Share on other sites

  • Moderators

Closest i could get... I can't figure out how to get it to match the punctuation at the end though.

#include <Array.au3>
$String = "Parts needs to be replaced per customer request."

$Result = StringRegExp($String, ".{0,35}\b", 3)

_ArrayDisplay($Result)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

:P ?

i think he wanted it to split the string by up to 35 characters, but preserve the words. I ran yours, but i got the result he said he didn't want:

Line 1: Parts needs to be replaced per cust

Line 2: omer request.

if you do it my way it comes out the way he says he DID want it:

Line 1: Parts needs to be replaced per

Line 2: customer request.

Edited by Paulie
Link to comment
Share on other sites

if you always want to break the line after several chars u can use:

$line1 = Stringleft(Parts needs to be replaced per customer request, 30)

$line2 = Stringtrimleft(Parts needs to be replaced per customer request, 31) ; + 1 to remove space.

if i haven't understood what u need... ask again ;)

EDIT:

i've read some more and here is a better one for u...

$str = @YOUR LINE THAT U NEED TO PARSE !

While stringlen($str)>35

$cut = Stringleft($str, 35)

$str = StringTrimLeft($str, 35)

iniwrite(..,...,...,...,$Cut)

Wend

this should cut the string and save it to an ini in cuts of 35...

That's what I have now and it's not exactly what I wanted. Thanks for the reply though.

Closest i could get... I can't figure out how to get it to match the punctuation at the end though.

#include <Array.au3>
$String = "Parts needs to be replaced per customer request."

$Result = StringRegExp($String, ".{0,35}\b\.?", 3);edit for punctuation

_ArrayDisplay($Result)oÝ÷ Ûú®¢×£
÷ß}÷N¬¢w°¢¹,¶°k"0j{m¡·jëz÷§ò"Énuëayˬ¶­©Ý±©^²Ýz¸­²Ø¥
+Á1jjey«­¢+ØÀÌØíMÑÉ¥¹ôÅÕ½ÐíAÉÑ̹ÌѼÉÁ±ÁÈÕÍѽµÈÉÅÕÍи
ÕÍѽµÉ%èÔØàÐM±Í=ÉÈèÄÈÌÐÔØÅÕ½Ð

It works!!! :P

Thank you, thank you.

One more question. Can you explain in plain English what's going on here: ".{0,35}\b\.?"

Just so I can grasp the concept better just in case I'd need to tweak it later on.

Thanks in advance.

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

Thank you, thank you.

One more question. Can you explain in plain English what's going on here: ".{0,35}\b\.?"

Thanks in advance.

I can try ;)

Ok, lets break this down into it's parts:

  • .
  • {0,35}
  • \b
  • \.
  • ?
#1 is a period, if you look at the helpfile, you will see that a period will match any character except a newline by default.

#2, is a repetition indicator showing that the "." (a.k.a. any character) will match as many times as possible up to 35. This means it will match as close to 35 characters from the string as possible.

#3 is "\b", this indicates a word border. Meaning it will only match at the beginning or end of a word. So it's looking for as many characters as possible before the next word will put it over 35.

#4 is "\.". In #1 we determined that a period will match any character, but this is here to catch the punctuation at the end of the sentence. so we use the escape character "\" in fromnt of the period to say that we only want it it match an actual period.

And #5, the "?"on the end simply says that the escaped period in #4 may or may not exist, because it doesn't exist at every word border, only at the ends of sentences.

That about the best i can do. I hope it heps some :P

Edited by Paulie
Link to comment
Share on other sites

I can try ;)

Ok, lets break this down into it's parts:

  • .
  • {0,35}
  • \b
  • \.
  • ?
#1 is a period, if you look at the helpfile, you will see that a period will match any character except a newline by default.

#2, is a repetition indicator showing that the "." (a.k.a. any character) will match as many times as possible up to 35. This means it will match as close to 35 characters from the string as possible.

#3 is "\b", this indicates a word border. Meaning it will only match at the beginning or end of a word. So it's looking for as many characters as possible before the next word will put it over 35.

#4 is "\.". In #1 we determined that a period will match any character, but this is here to catch the punctuation at the end of the sentence. so we use the escape character "\" in fromnt of the period to say that we only want it it match an actual period.

And #5, the "?"on the end simply says that the escaped period in #4 may or may not exist, because it doesn't exist at every word border, only at the ends of sentences.

That about the best i can do. I hope it heps some :P

Ah it makes sense now. Thank you, that will help me a lot since I have more situations where I'd be using StringRegExp, i.e. Making AutoIT read the custom name of the excel active sheets and identifying it if it's a "Cover" sheet or a "Billing" sheet.

[font="Georgia"]Chances are, I'm wrong.[/font]HotKey trouble?Stringregexp GuideAutoIT Current Version

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...