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

RegExp : using end-of-line in a pattern

Hi to all,
I would like to get field values from a multi-line text.

For instance :
"
Name: Mr Smith
Address: Paris
"

I've tried to use (I'm a total regexp newbie, so I took it from the doc):
eregi("Name: (.*)\n",$VariableHoldingTheText,$parts);

Problem: $parts[1] starts at the right place, but does not stop at the
end of the line. I get :
"Mr Smith
Address: Paris
"

Where am I wrong ? I've tried to google around, but didn't find anything
that could help me...

Thanks in advance for your help/hints/links !

BR
--
Damien
Sep 21 '05 #1
5 4141
Damien wrote:
Hi to all,
I would like to get field values from a multi-line text.

For instance :
"
Name: Mr Smith
Address: Paris
"


Im not sure what you want, but if you want to get the different lines in
different variables, then you can use the explode()-function to split
the string:

$lines = explode("\n", $string);

The above code would give an array $lines where each array-element
contains one line of $string.

If $string contains

"Name: Mr Smith
Address: Paris"

then $lines[0] contains "Name: Mr Smith" and $lines[1] contains
"Address: Paris".

More information on the explode()-function: www.php.net/explode

Zilla
Sep 21 '05 #2
Zilla a écrit :
(snip)
Im not sure what you want, but if you want to get the different lines in
different variables, then you can use the explode()-function to split
the string:

(snip)
Thanks for this. I have several values to look for, and I don't know in
what lines they are (imagine a form lost in the middle of a text). This
is why I did not break up the text in lines. I'd rather not look for
evey value in every line...

Any other idea ?
BR,
Damien
Sep 21 '05 #3
Damien wrote:
....
Thanks for this. I have several values to look for, and I don't know in
what lines they are (imagine a form lost in the middle of a text). This
is why I did not break up the text in lines. I'd rather not look for
evey value in every line...


I think I found out whats wrong. I think I have read some place that the
function looks for the largest possible pattern. So if you are looking
for some a letter followed by a : in the following:

"x:y:z"

it will return "x:y:" and not "x:" as you were looking for.

So in your example there must be a newline after "Adress: Paris" so it
takes this line too.

When that is said, i can't figure out just now, how to prevent it from
doing that. But I'll think about it and get back later if i figure it out.

Zilla
Sep 21 '05 #4
I would recommend using the Perl compatible regexp functions over the
POSIX ones. They're faster and more powerful. People also tend to be
more familiar with the Perl syntax.

The following snippet does what you want:

<?
$s = "
Name: Mr Smith
Address: Paris
";
preg_match_all('/^(.+?): (.+?)$/m', $s, $m, PREG_SET_ORDER);
foreach($m as $set) {
echo "{$set[1]} = {$set[2]}<br>";
}
?>

The m modifier at the end tells the regexp engine to treat newline as
end of line. The ^ assertion, which normally would only match the
beginning of the string, now match sees a match immediately following
at newline. The same for the $ end-of-line assertion.

The (.+?) parts mean capturing at least one character, with greediness
turned off. Greediness dictates whether the regexp engine will try to
find the longest possible match. We don't want it to be greedy here.
Given the string "Time: 8:00PM", for example, we want the engine to
match up the first colon and stop, instead of trying to find another
colon that'd yield a longer match.

In an actual programming situation I would use the following pattern to
deal with possible errant white spaces:

/^\s*(.+?)\s*:\s*(.+?)\s*$/m

Sep 21 '05 #5
Chung Leong a écrit :
I would recommend using the Perl compatible regexp functions over the
POSIX ones. They're faster and more powerful. People also tend to be
more familiar with the Perl syntax. Hi,
Thanks for this tip... I had gone for the first one in the PHP doc ;)

The following snippet does what you want:

(snip)

Thanks for the code and espescially for the time you took explaining it!
This will make me progress quite faster now ;)

I'll try you suggestion tomorrow first thing. I'll be glad to get rid of
all those substr and stristr...

Thanks again !
BR,
Damien
Sep 21 '05 #6

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

Similar topics

3
by: Jesper Stocholm | last post by:
I need to be able to detect URIs in some text and after this replace dem with HTML-anchors, that is http://www.tempuri.org/page.html should be replaced with <a...
20
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
6
by: Nick | last post by:
Hi, How can I check if a number exists by itself in this string by using the RegExp object? --- var mystring = "11,111,01,011"; var match = "1"; var re = new RegExp( match );
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
26
by: Matt Kruse | last post by:
Are there any current browsers that have Javascript support, but not RegExp support? For example, cell phone browsers, blackberrys, or other "minimal" browsers? I know that someone using Netscape...
9
by: shellon | last post by:
Hi all: I want to match a string that not ended with the string "welcome", how do write the RegExp? Appreciate your help!
0
by: problematic | last post by:
Hello guys Actually i dont know does mysql support prepare with regexp but i have this query. prepare stm from "select count(id) as total from products where content regexp ']?]' "; set @a =...
8
by: reflex | last post by:
Hi, i have script with pattern (href=?(.*)({1}+)?), where i match any occurence of url and replace hyperlink so i have only anchor in it. var regExp = /href=?(.*)({1}+)?/ig; var wholeContent...
2
by: Nathan Sokalski | last post by:
I have the following script that I am using to test some JavaScript RegExp code: function RE() { var testing1=new RegExp("*"); var testing2=new RegExp("{0,}"); var testing3=new RegExp("+");...
2
by: pjacobi.de | last post by:
Dear All, I'm looking for a function which, given a regexp re and and a string str, returns whether re won't match any string starting with str. (so it would always return False if str is ""...
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
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: 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:
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
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: 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.