3 Replies - 6519 Views - Last Post: 26 August 2012 - 01:32 AM

#1 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Perl regex question

Posted 25 August 2012 - 05:33 AM

I have a flat file that has strings of this kind :
[Set abc.*-def.*]
rule1
rule2
rule3
[Set def-abc.*]
rule1
rule2
rule3

I have a Perl module that reads in the above file, and given an input that matches the "set regex" applies the rules under it. So abc1-def would fall in the first category where as def-abc1 would fall into the second category.

If I want to have a rule that satisifes abc1-def*, abc2-def*, abc4-def*, abc5-def* but not abc3-def.*, Can I achieve that? Remember that the strings are in a flat file. I do not have access to the Perl file and hence I can only play around with this configuration file.

Of course, I can have abc3-def.* rule as the first one followed by what I have above. Am trying to see if I can avoid this explicit rule.

Is This A Good Question/Topic? 0
  • +

Replies To: Perl regex question

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8028
  • View blog
  • Posts: 13,741
  • Joined: 19-March 11

Re: Perl regex question

Posted 25 August 2012 - 08:07 AM

You want a rule that matches abcN-def.*, where N can be any char except '3'? You could come up with stupidly complex ways of solving the problem, I suppose, but the simplest rule is generally the best one. In this case, if there's a particular case you want to exclude, exclude it explicitly.
Was This Post Helpful? 0
  • +
  • -

#3 apaddobs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 16-May 12

Re: Perl regex question

Posted 25 August 2012 - 07:10 PM

View Postjon.kiparsky, on 25 August 2012 - 08:07 AM, said:

You want a rule that matches abcN-def.*, where N can be any char except '3'? You could come up with stupidly complex ways of solving the problem, I suppose, but the simplest rule is generally the best one. In this case, if there's a particular case you want to exclude, exclude it explicitly.


Yes. But I could have more such exclusions later on and hence the question to handle it through a regex.
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl regex question

Posted 26 August 2012 - 01:32 AM

View Postapaddobs, on 25 August 2012 - 01:33 PM, said:

If I want to have a rule that satisifes abc1-def*, abc2-def*, abc4-def*, abc5-def* but not abc3-def.*, Can I achieve that?


Sure, you can do that with character classes if you have a set of allowed characters or negated character classes if you have a set of disallowed characters.

Normal character class allowing only 1, 2, 4, or 5 in that position:
abc[1245]-def.*


Negated character class allowing any character except 3 in that position:
abc[^3]-def.*


Note, however, that this is a general-case answer. It may or may not actually work in your particular situation because the regex is being interpreted by code that you don't have access to, so that code may modify the regex in some way before processing it (e.g., it may consider [ and ] to be "illegal characters" and remove them), which could prevent this from working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1