INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

using regular expressions to extract "adjacent" substrings

Share

using regular expressions to extract "adjacent" substrings

using regular expressions to extract "adjacent" substrings

(OP)
Suppose I have strings of the form:
xxxxxxxxxxZYYYxxxxxx

where x can be any character, Z is some specific character, and Y is a certain character type (i.e. digits) of arbitrarily length. I want to exploit the fact that substring Y...Y always appears after Z, and write a regular expression that extracts Y.

Thank you in advance!

RE: using regular expressions to extract "adjacent" substrings

Something like this?

CODE --> Perl

$ echo xxxxxxxxxxZYYYxxxxxx xxxxxxxxxxZYYxxxxxx | perl -ne 'while (/\S+Z(Y+)\S+/g) { print "$1\n";}'
YYY
YY

Annihilannic.
tgmlify - code syntax highlighting for your tek-tips posts

RE: using regular expressions to extract "adjacent" substrings

(OP)
Actually, I was looking for a regular expression to extract Y...Y (as many consecutive instances of the variable of type Y following Z as there are).

Would just =~ /\S+Z(Y+)\g
print $1

work (it doesn't in the code).

Thanks, Max
 

RE: using regular expressions to extract "adjacent" substrings

That expression has exactly the same result for me when applied to the same test above.  To me it sounds like I'm outputting exactly what you're asking for... I'm not sure I understand your requirements clearly?  Do you have an example of the input data and what you want to extract from it?  Also... where do you want to extract it to?

Annihilannic.
tgmlify - code syntax highlighting for your tek-tips posts

RE: using regular expressions to extract "adjacent" substrings

(OP)
For one thing, I only want to extract the YY..Y part of the string. The regular expression should contain a statement that identifies the variable of type Y by its adjacency to Z, but only returns the Y.

RE: using regular expressions to extract "adjacent" substrings

Err... yeah... that's pretty much how I understood it... and it's exactly what my example does?   

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: using regular expressions to extract "adjacent" substrings

Perhaps you just wanted this one?

CODE

my$mys='xxxxxxxxxxZ123xxxxxx xxxxxxxxxxZ09xxxxxx';
my@mtc=$mys=~/Z(\d*)/g;
print"@mtc\n";
Replace the star with a '+' if you don't want to capture strings of zero length.

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close