473,324 Members | 2,548 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,324 software developers and data experts.

Regular expression Match :

123 100+
Hey all,
I have some data like mg-st-005.xyz.pqr.dyd .
I want to have the string mg-st-005 ( string before the first "."(dot)).
My preg_match does not work properly....
can you please help me?
Thanks...
Jun 27 '08 #1
12 1466
what is 'preg_match' ie what is the code and its purpose?
are you just after code that will retrieve the string information before the first '.'?
Jun 27 '08 #2
ajd335
123 100+
what is 'preg_match' ie what is the code and its purpose?
are you just after code that will retrieve the string information before the first '.'?
hey leeogrady,
yes, I just wanted to have the string before the first ".".
And for that I used preg match..
My code was,
Expand|Select|Wrap|Line Numbers
  1. preg_match("/(.*)$./",$hostname,$matches);
  2.  
where host name is the mg-st-005.xyz.pqr.dyd
Thanks...
Jun 27 '08 #3
I am still not sure what preg_match is, but i think there is a better way...
I would use strtok() a built in function to PHP
the code is something like:
Expand|Select|Wrap|Line Numbers
  1. $delims = "/(.*)$/";
  2. $hostname = "mg-st-005.xyz.pqr.dyd";
  3.  
  4. $word = strtok($hostname, $delims);
  5. if($word == "mg-st-005") {
  6.   'IT IS A MATCH!
  7. }
  8.  
I hope this helps
Lee
Jun 28 '08 #4
Markus
6,050 Expert 4TB
I am still not sure what preg_match is, but i think there is a better way...
I would use strtok() a built in function to PHP
the code is something like:
Expand|Select|Wrap|Line Numbers
  1. $delims = "/(.*)$/";
  2. $hostname = "mg-st-005.xyz.pqr.dyd";
  3.  
  4. $word = strtok($hostname, $delims);
  5. if($word == "mg-st-005") {
  6.   'IT IS A MATCH!
  7. }
  8.  
I hope this helps
Lee
Would explode() not work?

[php]
$hostname = explode('.', 'mg-st-005.xyz.pqr.dyd');

echo $hostname[0];
[/php]
Jun 28 '08 #5
Markus
6,050 Expert 4TB
I am still not sure what preg_match is, but i think there is a better way...
You've never used any of the preg_* functions? You use them in conjunction with regular expressions.

preg_match(), preg_replace(), etc.
Jun 28 '08 #6
No i havent!?! thanks for that markusn00b, i will look in to them!

Dam. Yes of course, explode() is what i was trying to think of but couldnt! strtok() was the only thing i could think of!
Jun 28 '08 #7
Markus
6,050 Expert 4TB
No i havent!?! thanks for that markusn00b, i will look in to them!

Dam. Yes of course, explode() is what i was trying to think of but couldnt! strtok() was the only thing i could think of!
When you get your head around regular expressions (I still haven't), you'll be able to be alot more effective in your programming.

Darn that explode()!
Jun 28 '08 #8
Using explode() is the most efficient. But here's how to do that with preg_match:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $hostname = 'mg-st-005.xyz.pqr.dyd';
  3.  
  4. preg_match("/^([^\.]+)\..+$/", $hostname, $matches);
  5.  
  6. echo $matches[1]; /* will print: mg-st-005 */
  7. ?>
  8.  
Jun 29 '08 #9
ajd335
123 100+
Thanks all,
The problem is solved..
Thanks for the help..
Jun 30 '08 #10
Markus
6,050 Expert 4TB
Thanks all,
The problem is solved..
Thanks for the help..
Glad we could all be of assistance!
Jun 30 '08 #11
pbmods
5,821 Expert 4TB
I know I'm coming in here a little late, but...
Expand|Select|Wrap|Line Numbers
  1. $custom = substr($hostname, 0, strpos($hostname, '.'));
  2.  
Jul 1 '08 #12
ajd335
123 100+
I know I'm coming in here a little late, but...
Expand|Select|Wrap|Line Numbers
  1. $custom = substr($hostname, 0, strpos($hostname, '.'));
  2.  
Hey Pbmod,
Thanks for the solution..
Jul 1 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
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...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.