473,382 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

regexp problem

Hi

I have an array with following values:
419;
20;
19;20;21;
18;30;
17;20;25;
16;26;
16;17;20;21;22;34

I would like to find out in how many keys there is "19;". If I simply search for
"19;" it will also match "419;" witch is of course wrong. I tried to write a
regexp but can't seem to pull it off. :-( Could somebody please assist me with
that regexp

thanks
gordan
Jan 12 '06 #1
5 1244
d
"Gordan" <go*********@torkul.hr> wrote in message
news:dq**********@news1.xnet.hr...
Hi

I have an array with following values:
419;
20;
19;20;21;
18;30;
17;20;25;
16;26;
16;17;20;21;22;34

I would like to find out in how many keys there is "19;". If I simply
search for "19;" it will also match "419;" witch is of course wrong. I
tried to write a regexp but can't seem to pull it off. :-( Could somebody
please assist me with that regexp

thanks
gordan


Why are you using a regular expression? Also, how are you iterating through
your array?

dave
Jan 12 '06 #2
Gordan wrote:
Hi

I have an array with following values:
419;
20;
19;20;21;
18;30;
17;20;25;
16;26;
16;17;20;21;22;34

I would like to find out in how many keys there is "19;". If I simply search for
"19;" it will also match "419;" witch is of course wrong. I tried to write a
regexp but can't seem to pull it off. :-( Could somebody please assist me with
that regexp


Someone will probably come up with a better regex but try:

<?php

$foo = array(
"20;",
"419;",
"19;20;21;",
"20;19;21;",
"21;20;19;",
"19;",
"194;",
"18;30;",
"17;20;25;",
"16;26;",
"16;17;20;21;22;34"
);

foreach ($foo as $bar) {
if (preg_match("/^19;|;19;/", $bar)) {
echo $bar."<br>\n";
}
}

?>

This matches:
19;20;21;
20;19;21;
21;20;19;
19;

--
Andrew @ Rockface
np: Naked Raygun - Metastasis [stopped]
www.rockface-records.co.uk
Jan 12 '06 #3
d
"Andrew @ Rockface" <an****@rockface-records.co.uk> wrote in message
news:dq**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...
Gordan wrote:
Hi

I have an array with following values:
419;
20;
19;20;21;
18;30;
17;20;25;
16;26;
16;17;20;21;22;34

I would like to find out in how many keys there is "19;". If I simply
search for "19;" it will also match "419;" witch is of course wrong. I
tried to write a regexp but can't seem to pull it off. :-( Could somebody
please assist me with that regexp
Someone will probably come up with a better regex but try:

<?php

$foo = array(
"20;",
"419;",
"19;20;21;",
"20;19;21;",
"21;20;19;",
"19;",
"194;",
"18;30;",
"17;20;25;",
"16;26;",
"16;17;20;21;22;34"
);

foreach ($foo as $bar) {
if (preg_match("/^19;|;19;/", $bar)) {
echo $bar."<br>\n";
}
}


/(?:^|;)19;/ will do it, too.
?>

This matches:
19;20;21;
20;19;21;
21;20;19;
19;

--
Andrew @ Rockface
np: Naked Raygun - Metastasis [stopped]
www.rockface-records.co.uk

Jan 12 '06 #4
THANK YOU both it works perfectly :-)

gordan
Jan 12 '06 #5

"Gordan" <go*********@torkul.hr> wrote in message
news:dq**********@news1.xnet.hr...
Hi

I have an array with following values:
419;
20;
19;20;21;
18;30;
17;20;25;
16;26;
16;17;20;21;22;34

I would like to find out in how many keys there is "19;". If I simply
search for "19;" it will also match "419;" witch is of course wrong. I
tried to write a regexp but can't seem to pull it off. :-( Could somebody
please assist me with that regexp

thanks
gordan


To count the number of times 19 occurs, something like this would work.

<?
$myarray = array(
"419;",
"20;",
"19;20;21;",
"18;30;",
"17;20;25;",
"16;26;",
"16;17;20;21;22;34"
);

$num = 19;

echo "There are/is ".count(preg_grep("/(?:^|;)$num;/", $myarray))."
occurrence(s) of $num.";
?>
Rich
Jan 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Magnus Lie Hetland | last post by:
I'm working on a project (Atox) where I need to match quite a few regular expressions (several hundred) in reasonably large text files. I've found that this can easily get rather slow. (There are...
0
by: Ed Leafe | last post by:
I recently upgraded to 4.1 alpha (MySQL 4.1.0-alpha-standard-log) on my Linux server, and came across a problem with a query that had been working in 3.23 that no longer worked in 4.1a. I've...
10
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following...
6
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
1
by: geos | last post by:
hello, I have the problem writing the regular expression to verify the valid system path in the way that RegExp.$1 has to contain path up to the parent folder of a file, and RegExp.$2 should...
6
by: Christoph | last post by:
I'm trying to set up client side validation for a textarea form element to ensure that the data entered does not exceed 200 characters. I'm using the following code but it doesn't seem to be...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
9
by: vbfoobar | last post by:
Hello I am looking for python code that takes as input a list of strings (most similar, but not necessarily, and rather short: say not longer than 50 chars) and that computes and outputs the...
2
by: Uldis Bojars | last post by:
Hi All, I have encountered problems with JS RegExp.exec() and can't find what is the problem. Could you help me? formRequest is a function that extracts some information from XMLHTTPRequest...
4
by: r | last post by:
Hello, It seems delimiters can cause trouble sometimes. Look at this : <script type="text/javascript"> function isDigit(s) { var DECIMAL = '\\.'; var exp = '/(^?0(' + DECIMAL
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.