Jump to content

RegExp help


Recommended Posts

How to extract from

wqer zxcv(dfg xcv (sdf))

"zxcv" and "xcv" using RegExp ?

thanks.

If that's the exact format for all the strings then this will do it.

#include<array.au3> ;; For _ArrayDisplay() only
$sStr = "wqer zxcv(dfg xcv (sdf))"
$aRegEx = StringRegExp($sStr, "(?i).*?\h+([\w]+)", 3)
If NOT @Error Then
    _ArrayDisplay($aRegEx)
Else
    MsgBox(0, "", "Oooops")
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thanks and sorry. It's will not work if string changes. I mean:

extract every first word, that is before "("

Is this what you mean?

$aRegEx = StringRegExp($sStr, "(?i).*?\h+([\w]+)(?:\(|\h)", 3)

If not then try to give us a couple examples of exactly what $sStr would contain and what you need returned.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Would that fit your bill?

#include <Array.au3>

Local $s = "wqer() zxcv(dfg xcv (sdf)) Return (seterror( 1, 0,Abs   (Sqrt(5))))"

$a = StringRegExp($s, '(?i)(?:(?:\A|[^a-z])(\w+)(?=\s*\())+', 3)
_ArrayDisplay($a)

In fact, it turned out a little bit more complicated than I first thought, but it seems to work as intended.

You might have to adjust your own definition of "word" and "space", but I'm sure you get the idea.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Yeah, it works as needed. Thank you !!

You're welcome with or without your God!

You see that I've taken steps to grab a "function name" at the beginning of string, or separated or not by whitespace(s) from the previous part. I'm sure you know enough to fine tune it to the definition your context imposes. The idea was to use forward assertion to match the "\s*\(" rather than simple matching, since the latter does "consume" matched chars that could possibly be triggering the beginning of a new "function name".

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

What do you want to take from it?

$sStr = "sldkfj sdf4g 555 dfg"
$aRtn = StringRegExp($sStr, "\b\w+\b", 3)

Will return an array where

$aRtn[0] = sldkfj

$aRtn[1] = sdf4g

$aRtn[2] = 555

$aRtn[3] = dfg

Edit: Removed case insensitivity since it's not required with this particular SRE

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I want to exclude groups, that contains only numbers, and take "words"(sldkfj) and "words with numbers"(sdf4g)

Like that?

#include <Array.au3>
Local $s = "wqer() 456() zxcv(dfg 123 xcv (sdf)) Return (seterror( 1, 0,Abs (Sqrt(5))))"
$a = StringRegExp($s, '(?i)\b([a-z][a-z0-9]+)\b', 3)
_ArrayDisplay($a)

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

#include <Array.au3>

Local $s = "wqer() 456() zxcv(dfg 123 xcv (sdf)) Return (seterror( 1, 0,Abs (Sqrt(5))))"

$a = StringRegExp($s, '(?i)\b([a-z][a-z0-9]+)\b', 3)

_ArrayDisplay($a)

yes, thanks Edited by Godless

_____________________________________________________________________________

Link to comment
Share on other sites

Little problem:

'(?i)\b([a-z][a-z0-9]+)\b'

not includes "_"

As you appear to be willing to capture AutoIt-style variables names, I've been surprised that the following works:

Local $_ = 5
Local $_8 = 5
Local $_a = 5
Local $8 = 3
Local $8_ = 3
Local $8a = 3
ConsoleWrite($_ & $_8 & $_a & $8 & $8_ & $8a & @LF)

In short a dollar sign followed by at least one of upper or lowercase ASCII letter, or decimal digit or underscore.

That's precisely the set that \w is accepting, so your regexp is simpler. If you combine variables and functions, you get this:

#include <Array.au3>
Local $s = "wqer( $_ ) 456() zxcv(dfg 123 $xcv ($sdf)) Return (seterror( $_1, 0,Abs (Sqrt($5))))"
Local $vars = StringRegExp($s, '(?i)(?<=[^[:word:]]|\A)(\$\w+)', 3)
_ArrayDisplay($vars)
Local $fcts = StringRegExp($s, '(?i)(?<=[^[:word:]$]|\A)([a-z_]\w+)', 3)
_ArrayDisplay($fcts)

Try it and let us know when it breaks!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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