4

Is it possible to match a certain pattern but return a different, arbitrary string (that is not necessarily embodied in the test string)? I want the regex to return the arbitrary string.

Embedded Perl code would do, or just handle the returning part with the surrounding programming language. But I am curious to know if it is possible to do just with a regex. Let me formulate it as a (wrong) if-statement pattern.

(?(?=test)"true"|"false")

I have no specific regex dialect in mind, but it would be great to be able to do this in a general-purpose language like C#, PHP, Perl, JavaScript or Python. So, please no dedicated software.


My understanding of regex tells me that you cannot return something which is not there (as revived by Jan), and strictly, a regex pattern returns only a true/false result saying whether it matched (as pointed out by Borodin). But still.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Your intention is not all that clear. Do you mean something like [this](https://3v4l.org/U9XUs)? – ishegg Oct 15 '17 at 07:14
  • I thought about named groups. But I would prefer it to have the arbitrary string in the (match) result. I do not want to achieve anything. I am just curious. – wp78de Oct 15 '17 at 07:23
  • Lookarounds come to my mind: you look for sth. which might or might not be there and could return other matched characters. Please be more precise on what you want though. – Jan Oct 15 '17 at 07:27
  • @Jan. Sure, but if that arbitrary string is not in the test string? – wp78de Oct 15 '17 at 07:28
  • 2
    @wp78de: To make myself clear, I thought of this: https://regex101.com/r/2OMoO1/1 it returns Berlin or London with the same expression. In general though you cannot return sth. which is not there. – Jan Oct 15 '17 at 07:30
  • @Jan I'm afraid that's most likely the answer: It is not possible. Feel free to post this as an answer. – wp78de Oct 15 '17 at 07:43
  • @wp78de: Strictly, a regex pattern *returns* only a true/false result saying whether it matched. How do you expect this string to be returned? – Borodin Oct 15 '17 at 10:04

3 Answers3

3

The non-destructive /r modifier returns the changed string, if the pattern matched

my $arb_str =  $string =~ s/$pattern/arbitrary-string/r;

Available since 5.14.0

zdim
  • 64,580
  • 5
  • 52
  • 81
  • But that's a substitution, not only a pattern. – simbabque Oct 15 '17 at 09:04
  • 1
    @simbabque It's a regex which returns an arbitrary string if a pattern matches. (Nearly a quotation of the question.) An "only-a-pattern" has a meaning only in the context of a regex of some sort; I think it doesn't matter that it is a substitution since it returns. I am indeed not entirely clear on what exactly is desired but this does what I read in the question, I think. – zdim Oct 15 '17 at 09:08
  • I agree with all your points, and I would have given the same answer. And yet, it bugged me that the actual regex is just the pattern. That `s///` is actually the substitution operator. So if we allow colloquial use of the term _regex_ I would say this answers perfectly. I'm probably just splitting hairs. ;-) – simbabque Oct 15 '17 at 09:13
  • 2
    @simbabque Right, it does (duly) get into semantics; what _exactly_ is asked for? I'd consider the pattern itself not to have an operable meaning in the code (it does for the engine); different things happen with `m/` and `s/` once it matches. If we were to expect a (captured) pattern to "return", it is the `m/` that returns (well, or `s///r`). Thus I'd go with the wider meaning of "regex." – zdim Oct 15 '17 at 09:23
  • @zdim I have to agree. When I started out to explore my options here I also considered a regex replace but rejected it. After your answer, I am more inclined to regard it as the best, maybe only option. – wp78de Oct 15 '17 at 22:12
3

Answer: you can not get anything out of a string with regular expressions which is not there. You may look for strings with lookaheads and can return other part of that string, e.g. with

\b\w+\b(?=\W*wp78de)

See a demo on regex101.com.


To have what you want you'll most likely need some sort of programming language:
if ($match) {
    return "some string here";
} else {
    return "some other string";
}
Jan
  • 42,290
  • 8
  • 54
  • 79
0

No, this is not a thing that regular expressions do. But then regular expression matches don't necessarily "return" anything. What you are asking is going to have its answer, if anywhere, in the details of the language using the regular expression. If you would explain your actual problem, and the context in which you are using a regular expression, you will have a much better chance of getting a helpful answer.

ysth
  • 96,171
  • 6
  • 121
  • 214