[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.2

Deceivingly Difficult Regex

Asked by soapergem in Regular Expressions, Perl Programming Language

Tags: regex, regular, difficult, expression

Hi experts,

I have some long lines of data, and in the middle of each of these lines are occasional pseudo-fixed-length numbers. By that I mean that they will always occupy a fixed number of characters (e.g. 5 chars) but they may or may not be padded on the left with spaces (e.g. 2 spaces and 3 digits). For example, I might have some lines of data like this (The pound signs represent other alphanumeric data; I'm just highlighting the portion that I'm referring to):

    #####12345#####
    #####  123#####
    ##### 1234#####

So you see the first line has some data followed by the number 12345 followed by some more data. The second line has some data, followed by 2 spaces and 3 digits, and then more data. Finally the third line has data, 1 space and 4 digits, and again more data.

I need a regex that will create a consistent back reference to *just the number part* of that and exclude the spaces. My first thought was, of course, to use something along the lines of this:

    /\s*(\d+)/

If that worked, the number would be put into backreference  \1 . However, it doesn't always work, because the pound signs represent other bits of data which could possibly be numeric. I run into a problem when the data just beyond this number is also numeric--the expression above wouldn't be able to tell the difference. Example:

    abcde  1234a4a4

So this is the string abcde, followed by 2 spaces and then the number 123, followed by the string 4a4a4. I would want to just match 123, but my expression above would spill into the next piece of data and give me 1234.

So what I really want to do is something more like this (this is only a pseudo-regular expression):

    /\s*(\d{  (5 - number of spaces matched)  })/

Except I don't know if anything like that can be done with regex. I even thought of compiling a grouping of a lot of different possibilities OR'ed together, but if I do that, I'm not sure how to consistently retrieve the back reference there either. Something like this:

    /(?:(\d{5})|\s(\d{4})|\s{2}(\d{3})|\s{3}(\d{2})|\s{4}(\d))/

That would match perfectly every time and would not spill over into the following data, but it also creates a new problem: the number would be stored in either  \1 ,  \2 ,  \3 ,  \4 , or  \5  depending on how many digits it was. So if I go to do a replacement, I'm not sure which one to use. I would like it to always be in the same place so that I can actually do something with it.

Let me know if any of this is unclear. Thanks in advance!
[+][-]07/11/07 01:42 PM, ID: 19466587Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Regular Expressions, Perl Programming Language
Tags: regex, regular, difficult, expression
Sign Up Now!
Solution Provided By: mjcoyne
Participating Experts: 7
Solution Grade: A
 
[+][-]07/11/07 10:07 AM, ID: 19464544Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 10:24 AM, ID: 19464707Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 10:25 AM, ID: 19464714Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 10:30 AM, ID: 19464756Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 11:15 AM, ID: 19465146Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 11:49 AM, ID: 19465505Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 12:49 PM, ID: 19466061Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/11/07 01:28 PM, ID: 19466471Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07/11/07 01:30 PM, ID: 19466493Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 01:37 PM, ID: 19466555Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/11/07 01:44 PM, ID: 19466606Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/11/07 01:51 PM, ID: 19466671Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/11/07 02:20 PM, ID: 19466896Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07/11/07 03:08 PM, ID: 19467176Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/11/07 07:07 PM, ID: 19468218Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92