Jump to content

Regex help


dmob
 Share

Recommended Posts

I have these lines:

2.1. This is 1st line
2.2. This is 2nd line [5,4]
4.2.1 This is 3rd line [5.5]
4.2.2. This is 4th line [5½]

I need to extract the line number, the line text itself and the number(s) in square brackets.

My problem is with lines 2.1 and 2.2.... I can extract one or the other but I am battling to get both

This is the regex I am fumbling with:

(\d)\.?(\d)?\.?(\d)?\.?\s(.*)\[(.*)\]

I have spent hours messing around and would appreciate help getting it right.

Link to comment
Share on other sites

Does this work for you?

#include <Array.au3>

Local $s = _
    "2.1. This is 1st line" & @CRLF & _
    "2.2. This is 2nd line [5,4]" & @CRLF & _
    "4.2.1 This is 3rd line [5.5]" & @CRLF & _
    "4.2.2. This is 4th line [5½]"

Local $a = StringRegExp($s, "(?m)^((?:\d+\.?)+)\h+([^\[\v]+)(?|\h+\[([^\]]+)\]|())$", 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

That's possible using a regexp but requires a fixed maximum number of captures setup in the pattern and unused captures will get useless blank stings. To keep flexible you have better time handling that in aftermath code, just like removing the final extra dot in 2.1.3.

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

http://regex101.com/ will tell you all you want. Set option g (global) which is the equivalent of our option 3.

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

#include <Array.au3>

Local $s = _
    "2.1. This is 1st line" & @CRLF & _
    "2.2. This is 2nd line [5,4]" & @CRLF & _
    "4.2.1 This is 3rd line [5.5]" & @CRLF & _
    "4.2.2. This is 4th line [5½]"

Local $a1 = StringRegExp($s, "(?m)^((?:\d+\.?)+)\h+([^\[\v]+)(?|\h+\[([^\]]+)\]|())$", 3)
$a2 = _ArrayDiv($a1, 3)

_ArrayDisplay($a2)

;====================================
Func _ArrayDiv($array, $div, $cols = $div)
   If Mod(UBound($array), $div) <> 0 Then Return SetError(1, 0, 0)
   If $cols < $div Then Return SetError(2, 0, 0)
   Local $tmp[UBound($array)/$div][$cols]
   For $i = 0 to UBound($array)-1 step $div
       For $j = 0 to $div-1
          $tmp[$i/$div][$j] = $array[$i+$j]
       Next
   Next
   Return $tmp
EndFunc

:D

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