Jump to content

Regular Expression to select text over multi-lines


Recommended Posts

Text in a file, read into var with fileread:

<>
<>
<>
<>
<
J please look
>
<>
<>
<>

Hi, 

I want  a RegExp to select around 'please', back to the previous < and forward to the next >.  I can select the line of text.  Then I add in (?s) and it selects the whole text.  I think I want to make it not greedy, (?U) , that seems to make it ungreedy after, but it still selects all the previous lines.

$sPattern = "(?s)<.*please.*>"            ; 1
$sPattern = "(?s)<(?U).*please.*>"        ; 2
$sPattern = "(?s)<(?U).*please(?U).*>"    ; 3
$sAry = StringRegExp($sHTML, $sPattern, 3)

 

Link to comment
Share on other sites

Mikell,  Thansks for that.  I've had a quick play with it and must finish now for today (UK 23:30) It provokes questions:

1 - the .* at each end, outside the <> - what are they doing? I don't want anything outside the <>.

2 - I'm using your pattern in StringRegExp, 3) and the selected text doesn't include the immediate <> (all the text up to those, I've added a few chars to make sure.  Why aren't the <> selected if they are in the pattern?  (This agrees with what gets replaced using your StringRegExpReplace).

Richard.

 

 

 

Link to comment
Share on other sites

This example allows for "please" being in first line or the last line.  And returns all of the previous line, and all of the next line of the "please" contained line, if they exist.

Note: In my example and Mikell example all the text in the "test" parameter of StringRegExpReplace() is matched with the regular expression pattern. So, the only text returned is in the "replace" parameter, which is "$1".  This is the first capture group which is referenced by the first back-reference, "$1".  The first capture group or the first back-reference is defined by the matching text that is matched after the first open bracket, traveling from left to right, and before the matching close bracket.

#cs
<>
<>
<>
<>
<
J please look
>
<>
<>
<>
#ce

;$str = FileRead("1.txt")
$str = StringRegExpReplace(FileRead(@ScriptFullPath), "^(?s).*#cs\s+(.+)\s+#ce.*$", "$1") ; Extract test string from this script.
;ConsoleWrite($str  & @CRLF)

$sFind = "please"
$res = StringRegExpReplace($str, '(?s).*?((\V*\v+)?\V*\Q' & $sFind & '\E\V*(\v+\V*)?).*', "$1")

ConsoleWrite($res & @CRLF)
MsgBox(0, "Results", $res)

 

Link to comment
Share on other sites

9 hours ago, RichardL said:

1 - the .* at each end, outside the <> - what are they doing? I don't want anything outside the <>.

The pattern represents the whole string and the part to grab is put inside brackets (capturing group). So the whole text will be replaced by the content of this group, which is backreferenced as "$1" as Malkey explained
 

9 hours ago, RichardL said:

2 -....  Why aren't the <> selected if they are in the pattern? 

because they are outside the brackets. Just move the brackets to include < and > in the group and they will be grabbed too

$str = FileRead("1.txt")
; get the wanted part
$res = StringRegExpReplace($str, '(?s).*(<.*?please.*?>).*', "$1")
; remove included newlines
$res = StringRegExpReplace($res, '\R', "")
Msgbox(0,"", $res)

Using StringRegExp, 3 is a little different. You must then specify that the chars to be grabbed around 'please' must not be < or > by using of a negated character class

$str = FileRead("1.txt")
; using StringRegExp w/ flag 3
$res = StringRegExp($str, '(?s)<[^<]*please[^>]*>', 3)
; remove newlines
$res[0] = StringRegExpReplace($res[0], '\R', "")
Msgbox(0,"", $res[0])

Edit
Please note that there are several ways to skin this cat  :)

Edited by mikell
Link to comment
Share on other sites

  • 2 weeks later...

It took me a few days to get back to this.  Your patterns worked well on the example text.  When I came to look at the actual text again, the 'not include' selection to prevent it including from the first <P needed to be a string <P, not just one char [^<].  I did some Googleing and it looked hard.  Then I realised I could limit the selection to only the immediately surrounding tags using .{1,90} instead of .* .  Not a very precise way to skin the cat but it's working.  I've learned a few things, thanks.

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

×
×
  • Create New...