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

Help needed in RegExp

I need to get the content present between .tc_XXXX_1(Rexp should be tc followed by the content followed by _and a digit) in the $var

XXXX can contain specialchar,Alphabets or Numerals. Not sure why $2 prints "n" in the below code.

Expand|Select|Wrap|Line Numbers
  1. $var="test.tc__na1me_1.20080616184340025";
  2. if($var=~/(tc)__([a-z,A-Z,0-9])/)
  3. {
  4. print $2;
  5. print $3;----> #Why no output for $3
  6. print "came";
  7.  
  8. }
  9.  
Output :
ncame

expected Output:
na1me

If it is na1_me also it should work.
Jun 17 '08 #1
4 1113
numberwhun
3,509 Expert Mod 2GB
Well, it looks like your regular expression really needs some help.

To begin with, you asked why $3 has nothing. That is because there is no $3. From the match you specified, you only have two sets of (). The first one is $1 and the second one is $2. You did not specify anything to be saved as a $3. You could re-write the match to be the following, but please keep in mind that this is untested and might need some tweaking:

Expand|Select|Wrap|Line Numbers
  1.  $var="test.tc__na1me_1.20080616184340025";
  2. if($var=~/^\d+_+(a-zA-Z0-9)+_+.+$/)
  3. {
  4. print $1;
  5.  
  6. }
  7.  
If the above code works, it will print out the na1me that you expected. Albeit, the code is a little greedy, but it will hopefully get the job done. If I have time to test it, I will do so and correct any errors. But, in the middle match (a-zA-Z0-9), you mentioned special characters, you would have to specify exactly which special characters to look for, or you could possibly do it like this: (\w\W)*
but again, that's untested and I don't have a sample containing special characters from you either.

Regards,

Jeff

**Update: Ok, that regex doesn't work. I notice now some issues with it, but don't have the time right now to look at it. If someone else has the answer for the OP, please post it.
Jun 17 '08 #2
Thanks. I thought $2 will print na1me but it prints only "n". I shouldnot have asked about $3. Also the code given by you is not working. I will check it out and see why it fails.

About special charac it can be any so we need to match with the following conditions to get "na1me" form the $var below

var="test.tc__na1me_1.20080616184340025";

charaqcters follwed after .tc_
characters before _1(_any digit) should give me exactly what I need Irrespective of it has numerals,Aplhabets or special characters in it.

Currently Iam not able to do this. Any help will be useful.
Jun 17 '08 #3
nithinpes
410 Expert 256MB
The following code will do the job.

Expand|Select|Wrap|Line Numbers
  1. $var="test.tc__na1me_1.20080616184340025";
  2. if($var=~/\.tc__([a-zA-Z0-9]+)_/)
  3. {
  4. print $1;
  5.  }
  6.  
Jun 17 '08 #4
KevinADC
4,059 Expert 2GB
if there can be any non space characters between tc_ and _(digit):

Expand|Select|Wrap|Line Numbers
  1. if ($var =~ /\.tc_+(\S+)_+\d/) {
  2.    print $1;
  3. }
  4.  
if you know the exact amount of underscores there should be in the pattern remove '_+' and replace them with the number of underscores.
Jun 17 '08 #5

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

Similar topics

4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
5
by: Iain Downie | last post by:
Dear Group, I have a text field that I wish to cellect numbers of bird seen. However, some folk want to use 'c' before the number or '+' after to indicate a fuzzy number. I have tried creating a...
4
by: papillonimages | last post by:
Please can someone help me, I am trying to write a Regular Expression check for a valid (Windows) filename (cant believe I couldnt find one on the net already?!?!?!). Anyway, here is what I have...
6
by: paulsmith5 | last post by:
Hi, I have the following regular expression to validate a date-time field in European or d/m/y h:m:s format. ...
1
by: ric.castagna | last post by:
Greetings, all... I've got an issue that I'm trying to solve and RegExp looks to be my only avenue. Background: We are using a popular third party control for input into a textarea. By...
2
by: Tamas Nyilanszky | last post by:
hi, i need to filter (e.g.) the number 149, 1307 and 3100 from a line like this one: <td id=l2 title=149>1307/3100</td> so i will be able to split that string with reg.split() and receive...
7
by: VUNETdotUS | last post by:
How can I get the text after matching string is found: var str = "1111>AAAA<2222>BBBB<3333>CCCC"; if(str.indexOf("2222>")){ //how to get "BBBB" value following my "2222>" but before "<3333"...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
10
by: Stimp | last post by:
Hi all, given a string: " <img class='myclass' alt='my picture' src='image.jpg' /" (where alt and src are changeable) how do I create the following string:
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
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
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
0
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.