I'm really hoping I'm doing something silly and just can't see the problem... this would be trivial in Perl or other languages. Apparently backreferences are supported in grok https://grokconstructor.appspot.com/RegularExpressionSyntax.txt, but I can't make them work. I need to match on something basic:
identifier - Static Text identifier Rest Of Line
So my grok expression would be something like:
%{DATA:id_name} - Static Text \1 %{GREEDYDATA:rest_of_line}
But using http://grokdebug.herokuapp.com/ always produces a compile error. If I use any of the \k notation, same thing. I've tried wrapping the first variable in parentheses, double backslashes, random permutations, can't make it work.
Any help would be much appreciated. Thanks!
I don't think that the %{DATA:id_name} produces a named capture that you can use with custom regex back references. Instead, you could wrap %{DATA} in a named capture and then back reference to it, like so:
(?<id_name>%{DATA}) - Static Text \k<id_name> %{GREEDYDATA:rest_of_line}
Related
I'm upgrading a Symfony app with VSCode and I have numerous occurences of this kind of string :
#Template("Area:Local:delete.html.twig")
or
#Template("Group:add.html.twig")
In this case, I want to replace all the : with / to have :
#Template("Area/Local/delete.html.twig")
I can't think of doing it manually, so I was looking for a regular expression for a search/replace in the editor.
I've been toying with this fearsome beast without luck (i'm really dumb in regexp) :
#Template\("([:]*)"
#Template\("(.*?)"
#Template\("[a-zA-Z.-]{0,}[:]")
Still, I think there should be a "simple" regexp for this kind of standard replacement.
Anyone has any clue ? Thank you for any hint
You can use this regex with a capture group: (#Template.*):.
And replace with this $1/.
But you'll have to use replace all until there's no : left, that won't take long.
Just explaining a lit bit more, everything between the parenthesis is a capture group that we can reference later in replace field, if we had (tem)(pla)te, $1 would be tem and $2 would be pla
Regex!
You can use this regex #Template\("(.[^\(\:]*)?(?:\:)(.[^\(\:]*)?(?:\:)?(.[^\(\:]*)?"\) and replacement would simply be #Template\("$1/$2/$3
You can test it out at https://regex101.com/r/VfZHFa/2
Explanation: The linked site will give a better explanation than I can write here, and has test cases you can use.
I'm working on a Dart program that will parse data from an XML file. Unfortunately, the file is a bit of a mess, so I'm doing a ton of regex on it to get it into decent shape. Since Dart, like Javascript, doesn't have lookbehind functionality, I've been trying to use capturing parentheses along with the method to mimic lookbehind, to no avail. Dart doesn't like the syntax of it at all, just telling me that function is not defined for myClass
My XML file consists of strings in the following format:
<items>Chicken Sauces/ Creamy Curried Chicken Salad w/ Wild Rice</items>
When finished, I want that string to look like:
<items>Chicken Sauces| Creamy Curried Chicken Salad w/ Wild Rice</items>
I've tried calling this on the string: .replaceAll(new RegExp(r'(\w\s*)/(?=\s*\w)'),"$0|$1")
but that gives me errors saying "Expected an identifier" for $0 and $1. If anyone could offer me some pointers on how to properly use capturing parentheses, or a method to mimic lookbehind in Dart, along with the current lookahead, I'd be very grateful!
Assuming your regexp is correct you should use String.replaceAllMapped: .replaceAllMapped(new RegExp(r'(\w\s*)/(?=\s*\w)'), (match) => "${match[0]}|${match[1]}")
You might also be interested in String.splitMapJoin
Compeek already pointed out that the regexp probably won't do what you want, though.
I am trying to replace public methods to protected methods for methods that have a comment.
This because I am using phpunit to test some of those methods, but they really don't need to be public, so I'd like to switch them on the production server and switch back when testing.
Here is the method declaration:
public function extractFile($fileName){ //TODO: change to protected
This is the regexp:
(?<ws>^\s+)(?<pb>public)(?<fn>[^/\n]+)(?<cm>//TODO: change to protected)
If I replace it with:
\1protected\3\//TODO: change back to public for testing
It seems to be working, but what I cannot get to work is naming the replace with. I have to use \1 to get the first group. Why name the groups if you can't access them in the replacing texts? I tried things like <ws>, $ws, $ws, but that doesn't work.
What is the replacing text if I want to replace \1 with the <ws> named group?
The ?<ws> named group syntax is the same as that used by .NET/Perl. For those regex engines the replacement string reference for the named group is ${ws}. This means your replacement string would be:
${ws}protected${fn}\//TODO: change back to public for testing
The \k<ws> reference mentioned by m.buettner is only used for backreferences in the actual regex.
Extra Information:
It seems like Geany also allows use of Python style named groups:
?P<ws> is the capturing syntax
\g<ws> is the replacement string syntax
(?P=ws) is the regex backreference syntax
EDIT:
It looks my hope for a solution didn't pan out. From the manual,
A subpattern can be named in one of three ways: (?...) or (?'name'...) as in Perl, or (?P...) as in Python. References to capturing parentheses from other parts of the pattern, such as backreferences, recursion, and conditions, can be made by name as well as by number.
And further down:
Back references to named subpatterns use the Perl syntax \k or \k'name' or the Python syntax (?P=name).
and
A subpattern that is referenced by name may appear in the pattern before or after the reference.
So, my inference of the syntax for using named groups was correct. Unfortunately, they can only be used in the matching pattern. That answers your question "Why name groups...?".
How stupid is this? If you go to all the trouble to implement named groups and their usage in the matching pattern, why not also implement usage in the replacement string?
SomeRandomText=%EXAMPLE1%,MoreRandomText=%%ONE%%!!%%TWO%%,YetMoreRandomText=%%THREE%%%FOUR%!!%FIVE%\%%SIX%%
I'm in need of a regular expression which can pull out anything which is wrapped in '%%'- so this regular expression would match only the following:
%%ONE%%
%%TWO%%
%%THREE%%
%%SIX%%
I've tried lots of different methods, and am sure there is a way to achieve this- but i'm struggeling as of yet. I mainly end up getting it where it will match everything from the first %% to the last %% in the string- which is not what i want. i think i need something like forward lookups, but struggling to implement
You need a non-greedy match, using the ? modifier:
%%.*?%%
See it working online: rubular
This can also be done be restricting what is allowed between the %s.
%%[^%]*%%
This is more widely supported than non-greedy matching, however
note that this won't match %%A%B%%. Although, if necessary, this can be done with some modifications:
%%([^%]|%[^%])*%%
Or equivalently
%%(%?[^%])*%%
I need to match a substring in php substring is like
<table class="tdicerik" id="dgVeriler"
I wrote a regular expression to it like <table\s*\sid=\"dgVeriler\" but it didnot work where is my problem ?
You forgot a dot:
<table\s.*\sid="dgVeriler"
would have worked.
<table\s+.*?\s+id="dgVeriler"
would have been better (making the repetition lazy, matching as little as possible).
<table\s+[^>]*?\s+id="dgVeriler"
would have been better still (making sure that we don't accidentally match outside of the <table>tag).
And not trying to parse HTML with regular expressions, using a parser instead, would probably have been best.
I dont know what you want get but try this:
<table\s*.*id=\"dgVeriler\"