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
nimda
  • Members
  • 4368 posts
  • Last active: Aug 09 2015 02:36 AM
  • Joined: 26 Dec 2010
Don't use a RegEx.
n = 21
MsgBox % SubStr("00000" . n, -4)
untested

Gogo
  • Guests
  • Last active:
  • Joined: --
Using at first look-ahead and then look-behind we can make RegEx to find the same position twice.
code:= "
(
Gui,Add,Button,x9 y9 w100 h30,Button1 
Gui,Add,Button,x109 y9 w10 h30,Button2 
Gui,Add,Button,x9 y49 w70 h30,Button3
)"

MsgBox % RegExReplace(code,"i)\b(x|y|w|h)(?=\d{1,2}\b)|(?<=x|y|w|h)(\d)\b","$10$2")


Odlanir
  • Members
  • 775 posts
  • Last active: Mar 06 2014 11:02 AM
  • Joined: 07 Aug 2011
Exactly what I need.
Thank you Gogo. Look-ahead and Look-behind are not so easy to manage for my regex knowledge.
Win7 - Firefox 10.0.2 - AHK_L 1.1.07.00
Please bear with me and my English which is so bad at times that even I don't understand myself

MacroMan!
  • Members
  • 604 posts
  • Last active: Mar 20 2012 11:40 AM
  • Joined: 28 Aug 2009
Hi all,

Having a bit of trouble with this simple piece of RegEx:

fStr := RegExReplace(fStr, "i)<p.*>", "")

It always returns an empty string. I've checked and Errorlevel is set to 0 after this has run, so I'm not sure what is wrong with it.

I'm attempting to replace all occurrences of <p> and <p id="174939"> with an empty string.

David
What ever happened, happened.

  • Guests
  • Last active:
  • Joined: --
fStr = aaa<p>abbbb<p id="174939">asdfadsa<p>dasgfaer<p class="dsaf">

fStr := RegExReplace(fStr, "i)<p.*?>", "")

msgbox % fStr


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

fStr := RegExReplace(fStr, "i)<p[^>]*>")

Or this:

fStr := RegExReplace(fStr, "i)<p(?: id=""174939"")?>")


leehong1
  • Validating
  • 403 posts
  • Last active: Aug 11 2016 11:25 AM
  • Joined: 24 Nov 2008
html....

TBODY>
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>8
<TD>8
<TD>3.95
<TD class=RIGHT>55/15
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>18
<TD>18
<TD>4.00
<TD class=RIGHT>120/20
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>30
<TD>30
<TD>4.15
<TD class=RIGHT>55/15
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>672
<TD>672
<TD>4.20
<TD class=RIGHT>120/30
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>25
<TD>25
<TD>4.20
<TD class=RIGHT>57/15
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>735
<TD>735
<TD>4.20
<TD class=RIGHT>56/15
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>430
<TD>430
<TD>4.25
<TD class=RIGHT>55/15
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>1</B>
<TD>848
<TD>848
<TD>4.30
<TD class=RIGHT>120/30


this regexmatch gives me only the last match.....
RegExMatch( show, "s)<TD>(1).*<B>(1).*<TD>(\d+).*<TD>(\d+).*<TD>(\d+).*RIGHT>(\d+/\d+)", h1d)
MsgBox, %h1d1%  %h1d2%  %h1d3%  %h1d4%  %h1d5%  %h1d6%

and this gives me nothing....

<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)>
<TD>1
<TD><B>4</B>
<TD>848
<TD>848
<TD>3.80
<TD class=RIGHT>120/30

RegExMatch( show, "s)<TD>(1).*<B(4).*<TD>(\d+).*<TD>(\d+).*<TD>(3.80).*RIGHT>(120/30|120/0)", h1d)
MsgBox, %h1d1%  %h1d2%  %h1d3%  %h1d4%  %h1d5%  %h1d6%


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Are you trying to get all the values out of the cells in the table?

html=
(
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>8 
<TD>8 
<TD>3.95 
<TD class=RIGHT>55/15 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>18 
<TD>18 
<TD>4.00 
<TD class=RIGHT>120/20 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>30 
<TD>30 
<TD>4.15 
<TD class=RIGHT>55/15 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>672 
<TD>672 
<TD>4.20 
<TD class=RIGHT>120/30 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>25 
<TD>25 
<TD>4.20 
<TD class=RIGHT>57/15 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>735 
<TD>735 
<TD>4.20 
<TD class=RIGHT>56/15 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>430 
<TD>430 
<TD>4.25 
<TD class=RIGHT>55/15 
<TR onmouseover=HL(this,1) onclick=ePL(this) onmouseout=HL(this,0)> 
<TD>1 
<TD><B>1</B> 
<TD>848 
<TD>848 
<TD>4.30 
<TD class=RIGHT>120/30
)
html :=	RegExReplace(RegExReplace(html,"<TR.*?>","&"),"<.*?>")
Loop, parse, html, &
{
	table .=	(!table ? "" : "`n")
	Loop, parse, A_LoopField, `n
		table .=	(A_Index=1 ? "" : "`t") A_LoopField
}
MsgBox %	table


leehong1
  • Validating
  • 403 posts
  • Last active: Aug 11 2016 11:25 AM
  • Joined: 24 Nov 2008
I loop thru all data and retrive with certain criteria....
I now have a code that can get the data...but the one in red still not working....

RegExMatch( show, "s)<TD>(1).*?<B>(" A_Index ").*?<TD>(\d+).*?<TD>(\d+).*?<TD>(3.80).*?RIGHT.>*?(120/30|120/0)", h1d)
Msgbox, %h1d1% %h1d2% %h1d3% %h1d4% %h1d5% %h1d6%

how do i escape this in red

RegExMatch( show, "s)<TD>(1).*?<B>(" A_Index ").*?<TD>(\d+).*?<TD>(\d+).*?<TD>(3.80).*?RIGHT">*?(120/30|120/0)", h1d)



Morpheus
  • Members
  • 475 posts
  • Last active: Oct 21 2014 11:08 AM
  • Joined: 31 Jul 2008
Use two quotes. RIGHT""

leehong1
  • Validating
  • 403 posts
  • Last active: Aug 11 2016 11:25 AM
  • Joined: 24 Nov 2008
ok.....thanks

Gogo
  • Guests
  • Last active:
  • Joined: --
[ SOLVED ]
Maybe you remember that simple task with the complex solution:
How to match and replace any occurrence of 'cat' but only if there is no 'dog' before it in the same line?
I recently learned from www.pcre.org/pcre.txt about a verb (*SKIP). The solution is much simpler with this verb.

(*SKIP) signifies that whatever text was matched leading up to it cannot be part of a successful match.


This verb works only if the next match fails so to fail the match I used a dot (the dot will always fail after a greedy dot-star).
Text =
(
This [color=red]cat[/color] must become a fish.
These [color=red]cat[/color] and [color=red]cat[/color] too. This is a [color=red]dog[/color] so this [color=red]cat[/color] must remain a [color=red]cat[/color].
These [color=red]cat[/color] and [color=red]cat[/color] too. This is a [color=red]dog[/color] so this [color=red]cat[/color] must remain a [color=red]cat[/color].
)
MsgBox % RegExReplace(Text, "`a)cat|dog.*(*SKIP).","fish")


_guest
  • Guests
  • Last active:
  • Joined: --
text1 and text2 may contain any alphanumeric, special characters and spaces but not *
text3 may contain any alphanumeric, special characters, spaces and *
trying to get the number fields, easy
trying to get text1, text2 and text3
Haystack = 01*02*03*04*05*TEXT1*TEXT2*TEXT3
NeedleRegEx = (\d+)\*(\d+)\*(\d+)\*(\d+)\*(\d+)\* 

RegExMatch(Haystack, NeedleRegEx, var)
loop, 8
Output .= var%A_Index% "`n"
MsgBox % Output
thanks in advance for your help

  • Guests
  • Last active:
  • Joined: --
Haystack := "01*02*03*04*05*TEXT1*TEXT2*TEXT3"

NeedleRegEx := "^(\d)+\*(\d)+\*(\d)+\*(\d)+\*(\d)+\*([^\\]*)\*([^\\]*)\*(.*)$"



RegExMatch(Haystack, NeedleRegEx, var)

Loop, 8

	Output .= var%A_Index% "`n"

MsgBox % Output


_Guest
  • Guests
  • Last active:
  • Joined: --
sorry, did not work. try:
Haystack = 01*02*03*04*05*TEXT1*TEXT2*TEXT*3
var8 should = TEXT*3