Re: RegEx ignores space in pattern
Re: RegEx ignores space in pattern
- Subject: Re: RegEx ignores space in pattern
- From: "John W. Baxter" <email@hidden>
- Date: Fri, 19 Mar 2004 09:13:41 -0800
On 3/19/2004 8:46, "Hans Haesler" <email@hidden> wrote:
>
I'd like to match numbers which can be formatted like this:
>
12 34 56-78
>
123 456-78
>
1234 56-78
>
>
Using "RegEx Commands" this works fine:
>
---
>
set aString to "abc 12 34 56-78 def"
>
set testString to REMatch aString pattern "[0-9][0-9 ]+-[0-9][0-9]"
>
-->"12 34 56-78"
>
>
But... the text contains other numbers, which shouldn't be matched:
>
---
>
set aString to "abc12-34-567def"
>
set testString to REMatch aString pattern "[0-9][0-9 ]+-[0-9][0-9]"
>
-->"12-34"
>
The [0-9 ] part of the pattern says "match any character which is 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, or space". Then the + says "one or more of those".
So, [0-9 ]+ would match
"1234567890"
or " "
Or "72 8 "
Your pattern would work as you expect if the data contained only things like
"abc 12 34 56-78 def"
But it will also match other things.
How careful you need to be in a particular usage of regular expressions
depends on what you know about the possible input.
If the input were coming from a program known to produce only things like
"abc 12 34 56-78 def" and other things which don't match at all, your
pattern would be fine.
If the data comes directly from humans, you have to be much more careful.
--John
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.