Jump to content

Help with StringRegExp


Recommended Posts

Hi,

I think StringRegExp can might be able to help me to do what I want, there are a couple of points I need to clarify.

First question, the help states that if you set the flag to 1 or 3 when calling the function, then the results are returned in an array if the pattern is matched.

$vResult = StringRegExp($sTest, $sPattern,1)

So when the array is returned, how do you know how many matches have occurred?

I would have thought that $vResult[0] would contain the number of hits, and then $vResult[1] .. [n] would contain the entries, but it doesn't seem to work like that :whistle:

Second question, say I have a pattern that I want to match (e.g. 'tag') and then I want to capture all the adjacent text until a quote (") is reached. Can I do this with StringRegExp?

Or if not, can I capture the whole line of text - so that I can then parse that?

Sorry if these questions seem a bit dumb, but I have studied the help and read some posts on the forums, and the way that the function operates is not entirely clear to me.

Thanks

VW

Link to comment
Share on other sites

HI,

could you give an example. The string line and then what your expected result should be.

Thanks!

I think questions to this topic are per se not stupid. :whistle:

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

First question, the help states that if you set the flag to 1 or 3 when calling the function, then the results are returned in an array if the pattern is matched.

Second question, say I have a pattern that I want to match (e.g. 'tag') and then I want to capture all the adjacent text until a quote (") is reached. Can I do this with StringRegExp?

Or if not, can I capture the whole line of text - so that I can then parse that?

First answer: UBound().

Second answer:

$text = 'Hello World this tag will end with " but the text goes on'
$pattern = '^.*tag(.*)"'
msgbox(4096,"TEXT",$text)
$result = StringRegExp($text,$pattern,1)
_ArrayDisplay($result,"Result")

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

could you give an example. The string line and then what your expected result should be.

Hi Mega,

Thanks for offering to help.

The text file contains a number of strings that I want to extract.

For example say there is a line in the file that contains the text "image", if I specify "image" as the pattern then the function matches this OK, but I have two problems.

Problem 1.

If the pattern "image" is in the file more than once, then from my reading of the help the results should be returned in an array. But I cannot figure out to query the array to determine how many matches occurred. How do I know how many entries were returned in the array?

Problem 2.

When you specify the pattern "image", that is all that is returned. But actually the information that I want to extract is after the keyword "image".

e.g. image=path\file

So I want to extract the string after the keyword, until a quote (") is reached. I think this will be possible using groups, after reading the tutorial though I am hoping that someone who is familiar with the function will say this can be achieved by doing "such & such". I think I can probably figure the groups syntax out through more trial-and-error but the way the results are returned in the array is not at all clear to me and isn't really covered in much detail in the help.

VW

Link to comment
Share on other sites

Maybe you did not see my post .... So, here again....

First question, the help states that if you set the flag to 1 or 3 when calling the function, then the results are returned in an array if the pattern is matched.

Second question, say I have a pattern that I want to match (e.g. 'tag') and then I want to capture all the adjacent text until a quote (") is reached. Can I do this with StringRegExp?

Or if not, can I capture the whole line of text - so that I can then parse that?

First answer: UBound().

Second answer:

$text = 'Hello World this tag will end with " but the text goes on'
$pattern = '^.*tag(.*)"'
msgbox(4096,"TEXT",$text)
$result = StringRegExp($text,$pattern,1)
_ArrayDisplay($result,"Result")

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

HI Kurt,

long time no see. :whistle:

What about this pattern ? Edit: Forget it : $pattern = 'tag(.*)"'

Edit2: this searches only the first tag, but when you need the last tag until " then your pattern is the one!

So long,

Mega

Edited by th.meger

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

HI Kurt,

long time no see. :)

yep... had some other things to do...

What about this pattern ? Edit: Forget it : $pattern = 'tag(.*)"'

Well, there is allways an easier way to write regexps. :whistle:

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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