|
Member 14835146 wrote: But it's very time-consuming
That is not specific. As in it takes 10 seconds? Or 10 hours?
Regexes meet specific needs but speed is not necessarily one of them. For starters a regex is always interpreted in the process. Even 'compiled' ones still end up in a form that is at best halfway to an actual compiled solution.
And your problem is in fact something that likely could be solved by real code. So that is likely something that would be faster.
But other than that it appears you might be attempting to do a regex solution for an entire file ('document') rather than doing it line by line. If you do in fact have lines which have a fixed number of timestamps then looping might provide a better solution especially if you can anchor the regex.
|
|
|
|
|
How to insert a space at the beginning of a line in a "for next" code loop (.NET regular expressions)
I am trying to add spaces at the beginning of lines matched with look arounds
for
line of code
line of code
line of code
line of code
next
and this is the output I want to get
for
line of code
line of code
line of code
line of code
next
Please help me with .NET regular expressions
modified 5-Jul-23 3:49am.
|
|
|
|
|
You did not supply a lot of information but based on your question you can use the 'Regex.Replace' method to insert a space at the beginning of each line -
Imports System
Imports System.Text.RegularExpressions
Public Module Program
Public Sub Main()
Dim code As String = "For i As Integer = 0 To 9" & vbCrLf & " Console.WriteLine(i)" & vbCrLf & "Next"
' Regex pattern to match the start of each line
Dim pattern As String = "(^|\n)"
' Insert a space at the beginning of each line
Dim myCode As String = Regex.Replace(code, pattern, " $1")
' Output the modified code
Console.WriteLine(myCode)
End Sub
End Module
|
|
|
|
|
Any of the many source code editors can do that with a couple of keystrokes. You need to explain where this text is coming from and what you are trying to do. Is this just a part of a file that you want to change, or something more complicated?
|
|
|
|
|
Piotr Przeklasa wrote: How to insert a space at the beginning of a line in a "for next" code loop (.NET regular expressions)
Nope. Wrong way to attempt this.
This is a common assumption that regex can handle this but the very nature of regex processing precludes it.
You need to do it with regular code.
The limits of using regex start showing up with recursion problems. For example the following.
for
line of code
for
line of code
next
line of code
line of code
next
|
|
|
|
|
I have different file name pattern as follows.
ABCD_ABCDEFGH_PARB_ALLB_CCYYMMDD-HHMMSS.TXT
SDCD_NKEDHEI_ALLIA_PARTN_CCYYMMDD-HHMMSS.TXT
UN_URKSLJIE_EXTRACT_DATA_ALLT_PART_CCYYMMDD-HHMMSS.TXT
And I was trying to use the following regex expression but it doesn't work all types of file names as above ...
^.*_(ALLB|ALLIA|ALLT|AMERI|BCBS|CCH|EASB|EASTP|EAST|SANDH|SANT|SANB|TRIB|TRILL|TRIT|UHC|VAYAH|VAYT|VAYB|WELLC)(?_PARTN|?_PART)_\d{8}-\d{6}"\.TXT$
Can someone help me with this?
|
|
|
|
|
Maybe the " in -- {6}"\.
Unsure what the second ? is doing in -- (?_PARTN|?_PART)
Edit: (?_PARTN|?_PART) should maybe be (_PARTN?)?
modified 23-May-23 16:10pm.
|
|
|
|
|
Use code tags when you post code on this site.
Why there is a double quote in what you posted?
|
|
|
|
|
I'm using the Redirection plugin by John Godley on our WordPress site, and I want to create a redirect rule that applies to multiple URLs, but also excludes 2 specific urls.
With a little help from Google as well as ChatGPT, I found that the syntax that should work is as follows:
location ~ ^/group/(?!members-only|harp-for-the-lord)(.*)$ {
return 301 /courses/$1;
}
However, apparently that code is supposed to be added to the NGINX configuration file, but I’ve never SSH’d into a server.
So I thought I'd ask here in case anyone could help me set this up so I can get the same result using the Redirection plugin.
Thoughts?
|
|
|
|
|
Challenge:
I have a file with genealogy information which I would like to extract (in Google Sheets) using regex.
Data:
One cell contains text information. Basically it is four main parts, two of which are optional and can have slightly different formats and contents
First comes always a number followed by a period. (This is the generation number.)
Second comes the name. It consists of one or more first and last names
These two are always there
They can be followed by birth and/or death information
If there is birth information, it always comes directly after the name and starts with "b. ".
It can have a date, and or a location
The date can be preceded by "circa", "before" and "before circa". It is then followed by either a 4 digit year, or more commonly by the month name, date, and year. Example: "March 4, 1888"
After the year might follow a location (free text)
If there is death information, it starts with "d. " and can contain the same information as above, i.e. a date and/or a location.
My best shot is close, but not handling the special cases of "before" etc too well:
=ARRAYFORMULA(IFERROR(SPLIT(REGEXREPLACE(A:A,"^(\d+)\.\s(.+?)(\s(b\.?\s?(\w+\s\d{1,2},\s\d{4})?,?\s?(.*?))?(; d\.\s(\w+\s\d{1,2},\s\d{4})?, \s?(.+)?)?)?$","$1|$2|$3|$4|$5|$6|$7|$8|$9"),"|")))
So the regex part of it is:
^(\d+)\.\s(.+?)(\s(b\.?\s?(\w+\s\d{1,2},\s\d{4})?,?\s?(.*?))?(; d\.\s(\w+\s\d{1,2},\s\d{4})?, \s?(.+)?)?)?$
It works well for entries like this one:
2. Gunnar Helg Andersson b. October 22, 1921, Ormöga No. 3, Bredsättra, Kalmar, Sweden; d. January 1, 2021, Köpingsvik
But not for entries like:
7. Kierstin Danielsdotter b. before circa 1706
9. Lussa Elofsdotter b. circa 1680; d. May 16, 1758, Bredsättra
7. Olof Jönsson b. 1742, Sverige (Sweden); d. September 4, 1811
9. Nils Knutsson b. circa 1676, Istad, Alböke; d. circa April 17, 1729
|
|
|
|
|
I have tried to decipher what your intent is. I can see you hope to get 9 fields by dividing the original information, but I fail to see where the different parts of the "born" and "death" fields occur.
What I have done thus far is to create a regex which gets the "record" number, the "name", the "birth" info if it exists and the "death" info if it exists. These last 2 fields can be further defined (and divided) if only I knew what your intent was.
Perhaps you can explain what should be in each of the 9 fields (if they exist). Perhaps show a "fully filled" out record as an example, then show what the result should look like.
But here is what I have thus far (this has been formulated on Notepad++):
^(\d+\.\s*)(.+?)(?=(?:b|d)\.)(b\.\s*.+?(?=(?:d\.|$)))?(d\.\s*.+?(?=$))?
To explain it we have:
^(\d+\.\s*) - start of line followed by number(s), a period and possible spaces
(.+?) - gather characters (as few as possible) until...
(?=(?:b|d)\.) - next character should be either a "b" or a "d" followed by a period. The (?: refers to a non-capturing group.
(b\.\s*.+?(?=(?:d\.|$)))? - gather characters until either a "d." follows or end of line.
(d\.\s*.+?(?=$))? - similar to previous line but for the "d." field. This assumes the "d." field will always be last.
Maybe it can give you some more inspiration. At the very least you can see how splitting the problem into smaller chunks may be beneficial. Even if you then have to further divide the "b." and "d." fields in a later step it may still be easier to define them.
Terry
|
|
|
|
|
It would be easier to write a string parsing routine of your own.
|
|
|
|
|
Richard MacCutchan wrote: It would be easier to write a string parsing routine of your own.
I strongly agree with this.
It is going to be easier to understand, easier to debug and quite possibly faster to run.
And just is case you think I have a bias I have been using regexes for 40 years extensively (via perl). Which is why I understand both their advantages and disadvantages.
|
|
|
|
|
jschell wrote: is case you think I have a bias Nothing would be further from my mind, even if you advocated a Regex. I respect everyone's opinions here; after all most people know lots of things that I do not.
|
|
|
|
|
Richard MacCutchan wrote: Nothing would be further from my mind
My post was phrased poorly since that part was not actually intended for you.
It was directed at the OP and/or other readers who might come across my comment.
|
|
|
|
|
Richard MacCutchan wrote: It would be easier to write a string parsing routine of your own.
Hi Richard,
Thanks for the tip. Although I don't fully understand what you mean with "string parsing routine", I solved the issue by writing a regex for each column needed instead of a "catch-all" regex. Perhaps that is what you meant.
|
|
|
|
|
No, my suggestion was to abandon the use of Regex patterns. You can easily split the string into an array of strings separated by spaces. All words before an entry of "b." are parts of the name. All words after the "b." and before "d." or the end of the text, relate to the birth date. All items after "d." relate to the date of death. And apart from anything else it makes your code much clearer.
|
|
|
|
|
I am looking for the regular expression for the following.
Microsoft.Sql/serversdatabases
That can match
Microsoft.Sql/servers/some-text/databases
Microsoft.Sql/servers/some--other-text/databases
etc...
The words
"Microsoft.Sql/servers/" &
"/databases" should be an exact match.. and where
*.* can match any text.
|
|
|
|
|
Try this:
Microsoft.Sql/servers/.*/databases
The .* will match an character followed by any other character, so you may need to modify that if you want to exclude any specific characters (e.g. any that are not valid in path names). You can get yourself a free copy of Expresso Regular Expression Tool[^] which will help develop REs.
[edit]
As suggested by k5054 below, the RE should have anchors so it is restricted to the actual text starting at Microsoft and ending at databases .
^Microsoft.Sql/servers/.*/databases$
[/edit]
modified 23-Mar-23 9:58am.
|
|
|
|
|
You might want to add anchors to that, too eg:
^Microsoft.Sql/servers/.*/databases$ so you don't also match my-Microsoft.Sql/servers/some-text/databases.info
Keep Calm and Carry On
|
|
|
|
|
Thanks, I forgot about those.
|
|
|
|
|
Member 15959121 wrote: Microsoft.Sql/servers/*.*/databases
You are misusing the asterisk. It matches the preceding element only. which in this case is the forward slash.
Moreover it matches zero or more. Which is probably not what you want.
However you also said...
"Microsoft.Sql/servers/" and "/databases" should be an exact match
So the first attempt at a fix, which is not correct, would look like the following.
(Microsoft.Sql/servers/)?.*/databases
But that is limited then because it does not match the second expression. You might think the following is a good idea but do NOT do this. You should never create a regex in which everything is optional.
(Microsoft.Sql/servers/)?.*(/databases)?
You would need to use an or ('|') with 3 expressions (match first, match last, match all) which to me is way too confusing from the maintenance standpoint.
It is not even clear to me if you have defined your match space. Presuming the following are NOT valid
Microsoft.Sql/servers/xxx
xxx/databases
Then I would do the following (pseudo code)
if match just: Microsoft.Sql/servers
else if match just: /databases
else match: Microsoft.Sql/servers/.*/databases
But additionally note even the above matches the following which is probably not what you want.
Microsoft.Sql/servers
|
|
|
|
|
First off, have you tried pre-pending TERM=dumb to your command string. That *should* remove all the control chars from the command output e.g.
[k5054@localhost ~]$ TERM=vt100 infocmp
# Reconstructed via infocmp from file: /usr/share/terminfo/v/vt100
vt100|vt100-am|DEC VT100 (w/advanced video),
am, mc5i, msgr, xenl, xon,
cols#80, it#8, lines#24, vt#3,
acsc=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, blink=\E[5m$<2>, bold=\E[1m$<2>,
clear=\E[H\E[J$<50>, cr=\r, csr=\E[%i%p1%d;%p2%dr,
cub=\E[%p1%dD, cub1=^H, cud=\E[%p1%dB, cud1=\n,
cuf=\E[%p1%dC, cuf1=\E[C$<2>,
cup=\E[%i%p1%d;%p2%dH$<5>, cuu=\E[%p1%dA,
cuu1=\E[A$<2>, ed=\E[J$<50>, el=\E[K$<3>, el1=\E[1K$<3>,
enacs=\E(B\E)0, home=\E[H, ht=^I, hts=\EH, ind=\n, ka1=\EOq,
ka3=\EOs, kb2=\EOr, kbs=^H, kc1=\EOp, kc3=\EOn, kcub1=\EOD,
kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA, kent=\EOM, kf0=\EOy,
kf1=\EOP, kf10=\EOx, kf2=\EOQ, kf3=\EOR, kf4=\EOS, kf5=\EOt,
kf6=\EOu, kf7=\EOv, kf8=\EOl, kf9=\EOw, lf1=pf1, lf2=pf2,
lf3=pf3, lf4=pf4, mc0=\E[0i, mc4=\E[4i, mc5=\E[5i, rc=\E8,
rev=\E[7m$<2>, ri=\EM$<5>, rmacs=^O, rmam=\E[?7l,
rmkx=\E[?1l\E>, rmso=\E[m$<2>, rmul=\E[m$<2>,
rs2=\E<\E>\E[?3;4;5l\E[?7;8h\E[r, sc=\E7,
sgr=\E[0%?%p1%p6%|%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;m%?%p9%t\016%e\017%;$<2>,
sgr0=\E[m\017$<2>, smacs=^N, smam=\E[?7h, smkx=\E[?1h\E=,
smso=\E[7m$<2>, smul=\E[4m$<2>, tbc=\E[3g,
[k5054@localhost ~]$ TERM=dumb infocmp
# Reconstructed via infocmp from file: /usr/share/terminfo/d/dumb
dumb|80-column dumb tty,
am,
cols#80,
bel=^G, cr=\r, cud1=\n, ind=\n,
[k5054@localhost ~]$ In general you can set any environment variable this way, so you might do something like
LD_LIBRARY_PATH=/home/k5054/lib DEBUG=1 ./foo Which would add LD_LIBRARY_PATH and DEBUG variables to the environment, but only for the duration of the given command.
But on to your problem. Assuming you've managed to remove your control characters, what it looks like you want to do is to match any line that does not have an option to it. Based on what you have here, you could match on any line that does not contain either a '[ ' (i.e. a required argumetn) or a '< ' (i.e. an optional argument). So the regex for that would be [^<\[] . Note we need to escape the opening square bracket, otherwise its treated as special character, and that means the regex won't compile.
<h1>include <regex></h1>
regex admin_cmds{"^.+[\\[<].+$"}; if ( ! regex_match(text, admin_cmds ) {
}
You could probably pull out the actual strings for the command and the options etc if you use subgroupings, (patter) , but I'll leave that as an exercise for you and the documentation: [std::regex_match - cppreference.com](https://en.cppreference.com/w/cpp/regex/regex_match)
Keep Calm and Carry On
|
|
|
|
|
A Bibtex file is a structured file (I have shown an example of two records at the end).
I would like to extract the 'keys', which is the text between an '@' and a comma but only get the text AFTER the '{'
So, in the line
@Article{m2023a,
it would return 'm2023a'
.. failing that, I could just get all those lines and then do another regex to further refine.
The best I have come up with so far is:
/@([^,]*)\,/
but I can't help feeling that there is a better way, and even this is not quite right.
An example of a Bibtex file is (this is two records, there could be hundreds):
@Article{m2023a,
author = {S. Macdonald},
journal = {Social Science Information},
title = {The gaming of citation and authorship in academic journals: a warning from medicine},
year = {2023},
doi = {10.1177/05390184221142218},
issue = {In Press},
}
@Misc{b2017a,
author = {S. Buranyi},
title = {Is the staggeringly profitable business of scientific publishing bad for science?},
year = {2017},
journal = {The Guardian, 27 June 2017},
url = {https:
}
|
|
|
|
|
So you just want the value between the { and the , on the lines starting with @ ?
That seems simple enough:
^@[^{]+\{([^,]+), Demo[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|