473,383 Members | 1,818 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,383 software developers and data experts.

Help with regular expression please

Trying to parse some strings and I could really use some help with a regular
expression (and how to test it in php).

My sample strings:

Bob 1/0/X0 134 128 1 5 Data
A comment line
Bob 1/0/X2 84 82 25 0 COMPARTMENT1-3
Bob 1/0/X1 1710 167 0 4 123 N/A
Bob 11/0/X3 84 7 0 6 Data

I'm trying to identify any string that starts with "Bob", then a space, then
a digit OR a space, then #/#/X#, three spaces, a 1 to 4 digit number, some
spaces, a 1 to 4 digit number, some spaces, a 1 to 4 digit number, some
spaces, a 1 to 4 digit number, some spaces, some alpha characters.

In my sample, the first, third and fifth strings would test positive.

I'm sure I've botched this up, but this is what I have so far.

if (
ereg('^Bob..[0-9]/[0-9]/X[0-9]\b[0-9]{1,4}(.{9,13})[0-9]{2,4}(.{9,13})[A-Za-z]*'
, $line ) ) { ...mycode... }
Jul 20 '07 #1
2 1249
Rik
On Fri, 20 Jul 2007 11:11:42 +0200, Noozer <do*******@me.herewrote:
Trying to parse some strings and I could really use some help with a
regular
expression (and how to test it in php).

My sample strings:

Bob 1/0/X0 134 128 1 5 Data
A comment line
Bob 1/0/X2 84 82 25 0 COMPARTMENT1-3
Bob 1/0/X1 1710 167 0 4 123 N/A
Bob 11/0/X3 84 7 0 6 Data

I'm trying to identify any string that starts with "Bob", then a space,
then
a digit OR a space, then #/#/X#, three spaces, a 1 to 4 digit number,
some
spaces, a 1 to 4 digit number, some spaces, a 1 to 4 digit number, some
spaces, a 1 to 4 digit number, some spaces, some alpha characters.

In my sample, the first, third and fifth strings would test positive.

I'm sure I've botched this up, but this is what I have so far.

if (
ereg('^Bob..[0-9]/[0-9]/X[0-9]\b[0-9]{1,4}(.{9,13})[0-9]{2,4}(.{9,13})[A-Za-z]*'
, $line ) ) { ...mycode... }

preg_match('%
^Bob #start of string
\s+ #arbitrary whitespace
[0-9]{1,2} #1 or 2 digits
/ #literal
[0-9] #digit
/X #literal
[0-9] #digit
\s+ #arbitrary whitespace
[0-9]+ #digit(s)
\s+ #arbitrary whitespace
[0-9]+ #digit(s)
\s+ #arbitrary whitespace
[0-9]+ #digit(s)
\s+ #arbitrary whitespace
[0-9]+ #digit(s)
\s+ #arbitrary whitespace
.+ #rest of data
%ix',$line);

Then again: http://www.php.net/sscanf

$lineparts = sscanf($line,'Bob %d/%d/X%d %d %d %d %d %s');
--
Rik Wasmus
Jul 20 '07 #2

"Noozer" <do*******@me.herewrote in message
news:i%_ni.132646$1i1.126404@pd7urf3no...
Trying to parse some strings .... My sample strings:

Bob 1/0/X0 134 128 1 5 Data
A comment line
Bob 1/0/X2 84 82 25 0 COMPARTMENT1-3
Bob 1/0/X1 1710 167 0 4 123 N/A
Bob 11/0/X3 84 7 0 6 Data

I'm trying to identify any string that starts with "Bob", then a space,
then a digit OR a space, then #/#/X#, three spaces, a 1 to 4 digit number,
some spaces, a 1 to 4 digit number, some spaces, a 1 to 4 digit number,
some spaces, a 1 to 4 digit number, some spaces, some alpha characters.

In my sample, the first, third and fifth strings would test positive.
preg_match('%^Bob\s+\d{1,2}/\d/X\d\s+\d{1,4}\s+\d{1,4}\s+\d{1,4}\s+\d{1,4}\s+[a-z]+.+%ix',$exstr);

I think previous reply may also match your example 4

hth
Alan
Jul 21 '07 #3

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

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: hillcountry74 | last post by:
Hi, I'm stuck with this regular expression from past 2 days. Desperately need help. I need a regular expression that will allow all characters except these *:~<>' This is my code in...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: sasifiqbal | last post by:
Hi All, I have following text Template that needs to be parsed using Regular Expression -- Test Template This is a Test Template for <#OrderNumber/> Sender Message is <#SENDERMESSAGE/>
5
by: John | last post by:
I am new in Regular Expression. Could someone please help me in following expression? 1. the string cannot be empty 2. the string can only contains AlphaNumeric characters. No space or any...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
14
by: JNariss | last post by:
Hello, I am fairly new to asp and jscript and am now in the process of learning some form validation. I am taking one step at a time by validating each field and testing it before moving onto...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
3
by: Mr.Steskal | last post by:
Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression Help -------------------------------------------------------------------------------- I need help writing a regular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.