2 Replies - 1076 Views - Last Post: 27 January 2012 - 02:28 AM

Topic Sponsor:

#1 eikkaj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 116
  • Joined: 20-December 08

Regex - extracting substrings

Posted 26 January 2012 - 12:52 PM

Wondering what the best way is to extract a substring at the end of a string that you don't necessarily know the length of. Something from the middle seems a lot easier just becuase you could use something like /THIS/../END/ and done!

Say the output is something like:

This is     1234      something       123awesome123


How would you extract the substring starting at 1 and ending at wherever it ends?

Is This A Good Question/Topic? 0
  • +

Replies To: Regex - extracting substrings

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 772
  • View blog
  • Posts: 6,133
  • Joined: 07-September 06

Re: Regex - extracting substrings

Posted 26 January 2012 - 01:10 PM

Yes, use the ^ operator to tell the pattern to start at the beginning of a line, and the $ operator to end at the end of a line.

So, if you only wanted to match "The" that started at the beginning of a line:
(^The)


is a sample pattern you could use to find it.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#3 dsherohman  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 199
  • View blog
  • Posts: 593
  • Joined: 29-March 09

Re: Regex - extracting substrings

Posted 27 January 2012 - 02:28 AM

- . will match any character (except a newline), or [^X] will match any character except X.

- * will match zero or more of whatever it follows, up to as many as are present.

Combining these two things, you can get the substring from the first 1 to the end of the line with
m/(1.*)/
or from the first 1 up to (but not including) the second 1 with
m/(1[^1]*)/

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1