Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 710
  • Last Modified:

php wildcard carachter, matching pattern with unknown amount of whitespaces in it in php and with ereg

I want to detect spaces in php with ereg. (it HAS to be ereg NOT preg and NOT eregi)

I want to match a pattern that is following:
1564        XYZYZYZ      5466
[OR]
ASD1 ZIA              657678767867
[OR]
6AFA515 XY-Z646-55S-DUZUZ      1ADADS

The amount of dots, numbers, chars+numbers and spaces can highly vary, but template doesn't.

Therefore I am doing:
if( (ereg('([A-Z0-9-]*[A-Z0-9])( *|\n )([A-Z0-9-]*[A-Z0-9])',$val,$alt_to_be_split)) &&  (ereg('( |  |   |    |\n )',$val,$alt_str_to_replace)) )

It works but I get troubles with:
if( (ereg('([A-Z0-9-]*[A-Z0-9])( *|\n )([A-Z0-9-]*[A-Z0-9])',$val,$alt_to_be_split)) &&  (ereg('( *|\n )',$val,$alt_str_to_replace)) )

Difference between 1 and 2 is the use of * as wildcard character to get unlimited amount of whitespaces.

(like in http://www.zend.com/zend/spotlight/code-gallery-wade5.php)

Any ideas of what it could be?
0
ChoobsTech
Asked:
ChoobsTech
  • 2
1 Solution
 
ChoobsTechAuthor Commented:
In the same topic: Why doesn't wildcard work for dots either when doing: (\.*)
0
 
sinisazgCommented:
Do you want to remove whitespaces? and just use the rest splitted?
I saw an error then in the basic regex, an extra *.
try this one:
([A-Z0-9-][A-Z0-9])( *|\n )([A-Z0-9-]*[A-Z0-9])
0
 
ixtiCommented:
Maybe I just didn't understood something, bt as far as I can understand it should be like this:

if (
    ereg('([A-Z0-9][A-Z0-9-]+)( +|\n *)([A-Z0-9][A-Z0-9-]+)', $val, $alt_to_be_split)
    &&
    ereg(' +|\n', $val, $alt_str_to_replace)
)
0
 
ChoobsTechAuthor Commented:
1-"*"
The * wildcard takes the char before it an illimited amount of times.

ereg("ap*le", "I saw the apple tree.");
This expression matches the letter a, followed by ZERO OR MORE of the letter p, followed by the letters l and e.

2-"+"
"The + meta-character provides almost the same functionality as the * character, except that the pattern must occur at LEAST ONCE"

In my case, I prefer the + character is suitable as I need at least 1 occurence of whitespace as was pointed ou by ixti.

To sinisazg:
-->([A-Z0-9-]*[A-Z0-9]) works fine, and just means that [A-Z0-9-] can occur ZERO OR MORE times followed by at least 1 time [A-Z0-9].
aces][1 WORD]

The problem was that ( *) looks for 0 or more whitespaces, therefore is buggy when placed alone as a match criteria alone in ereg...

To ixti:
Don't bother to agree with me! I already changed my mind... I was going to reply with vigourous denial to your proposition at your answer before heading to bed (was coding for 17 hrs in a row).
Few hours of sleep later, all I can say is: Why didn't I think of it!!!

Thnx a lot!

All points go to ixti. Feel free to make complaints in here if you feel this has been unequal.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now