Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Put here requests of problems with regular expressions


  • Please log in to reply
1074 replies to this topic
Thermopyle
  • Members
  • 135 posts
  • Last active: Apr 04 2012 04:04 PM
  • Joined: 27 Jul 2011

right. The Basic website...

Posted Image

IMO the offline is better since the searches return better results


...oh wow, I've been looking at the wrong documentation for months and never realized it. No wonder the documentation was so terrible. ^_^;;

leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008

120/5.6


this don't match......
(\d+\/\d+\.d+)
and this also do not match
([0-9]+\/[0-9]+\.[0-9]+)


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

120/5.6


this don't match......
(\d+\/\d+\.d+)
and this also do not match
([0-9]+\/[0-9]+\.[0-9]+)

The decimal must also be in the class
Try
(\d+\/[\d\.]+)
Just in case the 1st number is also a possible float
([\d\.]+\/[\d\.]+)

Posted Image

don't duplicate, iterate!


leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
TLM....tried both....

(\d+\/[\d\.]+)

([\d\.]+\/[\d\.]+)


still do not extract it.....
the data

125/0
120/5.6
55/8
29/11


i get 125/0

jaco0646
  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006

this don't match......

(\d+\/\d+\.d+)

\d+/\d+\.[color=red]\[/color]d+

and this also do not match

([0-9]+\/[0-9]+\.[0-9]+)

That works for me.

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

i get 125/0


Please show your code.

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

([0-9]+\/[0-9]+\.[0-9]+)
That works for me.

I guess the (untested) question is what happens if the 2nd number is not float?

edit

@leehong1, I thought you were after the entire string.
Ok, not a very good job for regex but
nM = 125/5.6
regexMatch( nM, "([\d\.]+)\/([\d\.]+)", match ) ; supports float on either side of slash.
msgbox % "#1: " match1 "`n`r#2: "  match2

Posted Image

don't duplicate, iterate!


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

He was showing the outcome based on my 2 needles.


Yes, but is it a single RegExMatch statement being used? Or is it RegExMatch in a while-loop? Is the haystack even what we think it is?

That is why I (for one) would like to see some code so we can see the entire context of how it's being used before we know what is or isn't working.

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
hello

why the following script does not work?

NewStr := RegExReplace(%Haystack%, (?!^(%list%)), Replacement = "")

Haystack is the outputvar from a previously stated FileRead
list is the output var from a previously stated FileRead that has undergone StringReplace

I used StringReplace to convert a list of numbers to this format: number1|number2|number3... etc
I want to use this in regex and delete all the lines in Haystack that do not start with any of the above numbers

thanks!

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

Is the haystack even what we think it is?

too true!
comment removed.

azure,
Regex parameters are no different than other function calls.
They do not need to be enclosed by double percents
and should have quotes around them if they are literal strings ( or "literal" needles ;) ).

Also using the parameter guide as the value ( ie Replacement = "" ),
is probably going to end up returning an error.
In this case leaving the replacement parameter blank is the equivalent of replace needle.

Untested:
NewStr := RegExReplace( Haystack, "(?!^(" list "))")
Wait, shouldnt your NewStr use a concat operator?

Posted Image

don't duplicate, iterate!


leehong1
  • Validating
  • 403 posts
  • Last active: May 19 2019 03:12 PM
  • Joined: 24 Nov 2008
I need to extract both data...
with this I get both data....
show =
(
<TD>1
<TD>3
<TD>28
<TD>28
<TD>8.60
<TD>31/11 

<TD>12
<TD>31
<TD>28
<TD>28
<TD>9.20
<TD class="RD RIGHT">31/1.1 
)

f1::
RegExMatch( show, "s)<TD>(\d+)`n<TD>(\d+)`n<TD>(\d+)`n<TD>(\d+)`n<TD>(\d\.\d+)`n<TD>(\d+\/\d+)", h1d)

MsgBox,  %h1d1%  %h1d2%  %h1d3%  %h1d4%  %h1d5%  %h1d6%

RegExMatch( show, "s)<TD>(\d+)`n<TD>(\d+)`n<TD>(\d+)`n<TD>(\d+)`n<TD>(\d\.\d+)`n<TD.*(\d+.\/\d+\.\d+)", h1d)

MsgBox,  %h1d1%  %h1d2%  %h1d3%  %h1d4%  %h1d5%  %h1d6%

return
how do I combine the above code to one REGEX code....
show =
(
<TD>1
<TD>3
<TD>28
<TD>28
<TD>8.60
<TD>31/11 

<TD>12
<TD>31
<TD>28
<TD>28
<TD>9.20
<TD class="RD RIGHT">31/1.1 
)
f2::

Loop, 2
{
RegExMatch( show, "s)<TD>(\d+)`n<TD>(\d+)`n<TD>(\d+)`n<TD>(\d+)`n<TD>(\d\.\d+)`n<TD.*(\d+.\/\d+\.\d+)", h1d)

MsgBox,  %h1d1%  %h1d2%  %h1d3%  %h1d4%  %h1d5%  %h1d6%
}

return


azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007

azure,
Regex parameters are no different than other function calls.
They do not need to be enclosed by double percents
and should have quotes around them if they are literal strings ( or "literal" needles ;) ).

Also using the parameter guide as the value ( ie Replacement = "" ),
is probably going to end up returning an error.
In this case leaving the replacement parameter blank is the equivalent of replace needle.

Untested:

NewStr := RegExReplace( Haystack, "(?!^(" list "))")
Wait, shouldnt your NewStr use a concat operator?


okay, that cleared a bit the RegExReplace syntax
you included spaces in quotes (eg: " list ", instead of "list") wouldn't that cause a different behaviour?

I tested the above code and the "Msgbox % NewStr" command returned the content of Haystack, instead of the content of Haystack minus the lines that start with each string of the "list"

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

I need to extract both data...

Wouldnt it be easier like this:
f1::
loop, parse, show, `n`r, % mList:=""
  regexMatch( a_loopfield, "([\d\./]+)", m ), mList .= m " "
msgbox % mList := regexReplace( mList, "\s\s+", " " ) ; clean up.
Pardon my "unorthodox" init and concat placements :cool:



you included spaces in quotes (eg: " list ", instead of "list") wouldn't that cause a different behaviour?

Here are the variables in your original assignment.
NewStr := RegExReplace(%[color=red]Haystack[/color]%, (?!^(%[color=red]list[/color]%)), Replacement = "")
What your not getting is the way variables must be used in function calls
hstk = catdogratbird
ndl  = dog
msgbox % regexMatch( hstk, "(?<=cat)%[color=red]ndl[/color]%(?=rat)" )   ; wrong
msgbox % regexMatch( hstk, "(?<=cat)" [color=red]ndl[/color] "(?=rat)" ) ; correct
Whitespace is not considered between the variable and literal strings,
unless you add spaces.

htms

Posted Image

don't duplicate, iterate!


azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007

What your not getting is the way variables must be used in function calls


okay, so a "function call" is anything inside parenthesis and in function calls, we place variables as is and not inside %'s

but why the code:
NewStr := RegExReplace( Haystack, "(?!^(" list "))")
does not work?

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

..why the code:
NewStr := RegExReplace( Haystack, "(?!^(" list "))")
does not work?

No insult intended but its probably your needle.
AFAICT your testing a negative look-ahead against nothing.

Please post all or part of your haystack ( with matches ).

Posted Image

don't duplicate, iterate!