Jump to content

StringRegExp


Recommended Posts

Is there an easy .... simple way to learn how to format Regular Expressions? I find myself needing to use them more often but almost always going loony in the process every time. If there is anyone who is fluent with using RegExp could you please offer some help. I am searching a string read from a file and I want to extract from it the following: Version 8/30/08 ...... the date can not be predicted and can be some other random date.... can you help me out with an expression for this. I just can't think anymore.

Link to comment
Share on other sites

  • Moderators

Is there an easy .... simple way to learn how to format Regular Expressions? I find myself needing to use them more often but almost always going loony in the process every time. If there is anyone who is fluent with using RegExp could you please offer some help. I am searching a string read from a file and I want to extract from it the following: Version 8/30/08 ...... the date can not be predicted and can be some other random date.... can you help me out with an expression for this. I just can't think anymore.

Must not have tried very hard, this is a very "basic" expression.

"Version \d+/\d+/\d+"

http://www.google.com/search?q=PCRE+Tutori...lient=firefox-a

To extract the date from it specifically, surround with parenthesis... ie.. StringRegExp($s_string, "Version (\d+/\d+/\d+)", 1)

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

Must not have tried very hard, this is a very "basic" expression.

"Version \d+/\d+/\d+"

http://www.google.com/search?q=PCRE+Tutori...lient=firefox-a

To extract the date from it specifically, surround with parenthesis... ie.. StringRegExp($s_string, "Version (\d+/\d+/\d+)", 1)

Thanks Smoke...... I tried something similar but I was not using the terminate "/" part of my problem I guess

Link to comment
Share on other sites

$string = "Version 8/30/08"
$pattern = "Version (\d+/\d+/\d+)"
$array = StringRegExp($string, $pattern, 3)
For $X = 0 to Ubound($array)-1
    ConsoleWrite("["&$X&"]: " & $array[$X] & @CRLF)
Next


;EXPLANATION:
;This is the constant, we know the word "Version" appears and is followed by a single space.
;Only items within parentheses will be captured into our array
$pattern = "Version\s"

;The left parentheses marks the beginning of our capture group.
;Items matching the pattern will consume one element in the output array.
;In this case we want the whole string MM/DD/YYYY or M/D/YY
;We can't rely on there being 2 digits for the month and day or 4 digits for the year.
$pattern &= "("

;This will match the month portion of our date.
;The first portion "\d" will match any digit 0-9.
;The plus symbol means it will match the previous character multiple times, which would allow a month of 99999...but we are keeping this simple
;The last character matches a literal slash, the slash is our only metric to know its a date format.
$pattern &= "\d+/"

;Match the day. Again, could be 0, 99999, etc. As long as there is at least one digit. Empty space will not provide a match i.e. 08//08
$pattern &= "\d+/"

;Final match. This will consume all digits after the last slash, so the year could be 2 digits or 4.
$pattern &= "\d+"

;Close capturing group with right parentheses.
$pattern &= ")"

$array = StringRegExp($string, $pattern, 3)

;Since StringRegExp returns an array, we cycle through each element and output to the console.
;The reason an array is returned is because there can be multiple matches i.e. your string is "Version 8/30/08 Version 8/18/09"
For $X = 0 to Ubound($array)-1
    ConsoleWrite("["&$X&"]: " & $array[$X] & @CRLF)
Next

Edited by weaponx
Link to comment
Share on other sites

Is there an easy .... simple way to learn how to format Regular Expressions? I find myself needing to use them more often but almost always going loony in the process every time. If there is anyone who is fluent with using RegExp could you please offer some help. I am searching a string read from a file and I want to extract from it the following: Version 8/30/08 ...... the date can not be predicted and can be some other random date.... can you help me out with an expression for this. I just can't think anymore.

Welcome to the same boat matey.

I try to stay away from RegEx as much as I can because it continues to kick my butt around.

Just to let you know, you're not alone. :)

EDIT: By the way, I personally use StringRegExpGUI.au3 to help me sort things out.

Edited by aslani

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

Link to comment
Share on other sites

  • Moderators

Welcome to the same boat matey.

I try to stay away from RegEx as much as I can because it continues to kick my butt around.

Just to let you know, you're not alone. :)

The problem is that you guys are trying to do something specific without any understanding of how things work.

If everyone just put their projects to the side for a couple of days (those people that do string manipulations), studied some of the online tutorials, and put those test codes they see/make into use with AutoIt's StringRegExp()... Few people would ever have to ask questions on it.

It's very much the same result with most things in life, when you have no knowledge of something, yet try to make use of it.

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

The problem is that you guys are trying to do something specific without any understanding of how things work.

If everyone just put their projects to the side for a couple of days (those people that do string manipulations), studied some of the online tutorials, and put those test codes they see/make into use with AutoIt's StringRegExp()... Few people would ever have to ask questions on it.

It's very much the same result with most things in life, when you have no knowledge of something, yet try to make use of it.

I can only speak for myself, so here goes. I use StringRegExp only if there's no other alternative. If I can StringSplit(), StringLeft(), etc...I'd use those instead. I know StringRegExp is very powerful in string manipulation, but for some reason, it's something I can't keep in my head...and unfortunately, many tutorial do not speak English.

Example:

(?U) = Invert greediness of quantifiers.

Seriously, what does that mean? :)

I'm simply admitting that RegExp is really hard to understand and the only way for me to understand it is thru examples by way of solving a problem.

Example, if someone asked how to manipulate a string, I read the answer and try to decipher what each matching characters do in order to manipulate the string. And more often, I get lost.

I use StringRegExpGUI.au3 but I still have to come here and ask because I just don't get it most of the time.

[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...