Page 1 of 1

Help with a regex expression

Posted: Wed Nov 13, 2002 3:57 pm
by lepreston
Hi all, I have an issue trying to use regex. I have a file that I am reading into memory. The process I am using is to read one line in at a time and use a regex expression to grep out the piece of data that I want to store in my database. Below is the format that I receive the lines in. The expression I have included below works but it falls short in a number of ways. First, any line might or might not end in a number preceeded by an x. It doesn't sort this out and I don't want the number and x in the captured string. Also, it is not properly accounting for the fact that there might or might not be apostrophes and dashes in the line which I do want to return. If either of these occur in the string it cuts the string off at that point.

| |-Slot 1: Fabric: Item 1 [19343]
| |-Slot 3: Trees: Items-with-dashes [10037]
| |-Slot 4: Item's with an apostrophe [10037]
| |-Slot 5: Three Word Items [10037]
| |-Slot 6: Items up to but not including x12 [10037]
| |-Slot 6: Example Two: Items up to but not including x26 [10037]


In the examples above the words in bold are the ones I want to return.

This is my current expression

preg_match("/:\s?([0-9*\-*a-zA-Z':\s]+)/",$read_line,$matches);

Thanks in advance. The help is appreciated.

Leo

Posted: Wed Nov 13, 2002 4:24 pm
by volka
I'm not sure if you want those [digits] parts. If not you may try

Code: Select all

$pattern = "/:\s*(.*)(?(?=\sx)\d*|\s\ї\d*\])/U";

Posted: Wed Nov 13, 2002 9:38 pm
by lepreston
Volka,

You are a genius :wink: Worked like a charm. Thanks a bunch. I worked on the silly line of code for about 6 hours :roll:

Posted: Thu Nov 14, 2002 3:38 am
by volka
the tricky part was to finally use the amount of whitespaces as condition for x{digits} or not ;)