Note that there are some explanatory texts on larger screens.

plurals
  1. POPERL: Using REGEX to match a string without the first token repeated in the string. (ABC, not AAA ABC)
    primarykey
    data
    text
    <p>I am relatively new to perl and I apologize in advance if this question is simple and I am missing something completely obvious, however, I've been looking around for the answer for a couple of days now and can't find a solution. </p> <p>I am trying to use a regular expression to match the FIRST instance where A is followed by C without another A in between A and C. Note that in this text string there are other instances following the first A.C combination that could also fit the A.C, (specifically note the ADC) Here is the text:</p> <pre><code>$text = "AAA ABC ADE AFG ADC AHI AJK AIZ XXB NBV"; </code></pre> <p>I first tried:</p> <pre><code>@finds1=$TEXT=~m/(A.*?C)/; $result = $finds1[0]; print "result = $result\n"; </code></pre> <p>This prints the following: </p> <pre><code>result = AAA ABC </code></pre> <p>When what I want is just:</p> <pre><code>result = ABC </code></pre> <p>Note that I am trying to create a regular expression that could be used where B could be any string of characters. For example ADC, AFGHJKC, AYUIOKJHGTC. </p> <p>I next tried to use a look-forward combined with an if then else statement. Here is the code:</p> <pre><code>@finds1=$TEXT=~m/(A(?(?!.*?A.*?C).*?C|Z{100}))/; $result = $finds1[0]; print "result = $result\n"; </code></pre> <p>The first part of the REGEX, (A, tells perl to find an occurrence of A. Once found, perl then processes the if then else statement where the conditional statement is that there are no instances of .<em>?A.</em>?A following the A, if none are found then perl looks for .*?C, if at least one is found, then it searches for 100 instances of Z. (my way of getting Perl to move on since neither in this text nor in the text I'm trying to parse are there 100 Zs.) </p> <p>This returns: </p> <pre><code>result = ADC </code></pre> <p>I have considered using a positive look-behind after identifying C for the first time. However, like I mentioned above, The number of characters between the first A.C combination without an A in between them is variable. As far as I know PERL can't do variable-length look-behinds. </p> <p>Any help or direction you could provide would be GREATLY appreciated!!</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload