Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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

MaxCarp (Programmer) (OP)
19 Aug 11 17:13
Suppose I have strings of the form:

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!
Annihilannic (MIS)
19 Aug 11 21:06
Something like this?

CODE --> Perl

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

tgmlify - code syntax highlighting for your tek-tips posts

MaxCarp (Programmer) (OP)
20 Aug 11 15:00
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
Annihilannic (MIS)
20 Aug 11 21:36
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?

tgmlify - code syntax highlighting for your tek-tips posts

MaxCarp (Programmer) (OP)
5 Sep 11 15:55
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.
Annihilannic (MIS)
5 Sep 11 20:45
Err... yeah... that's pretty much how I understood it... and it's exactly what my example does?   

tgmlify - code syntax highlighting for your tek-tips posts

prex1 (Programmer)
6 Sep 11 3:48
Perhaps you just wanted this one?


my$mys='xxxxxxxxxxZ123xxxxxx xxxxxxxxxxZ09xxxxxx';
Replace the star with a '+' if you don't want to capture strings of zero length.

Franco : Online engineering calculations : Magnetic brakes for fun rides : 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!

Back To Forum

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