|
I know I know. Yes, it is a challenge!
|
|
|
|
|
Hello!
Can someone give me a piece of advice on how to build a regex for the following conditions -- only one capital letter in a word (must be one) n no digits.
example pass -- Apple aPple giFt
example decline -- street agentORANGE sequence1122
|
|
|
|
|
Basically, don't use a regex: they are a powerful tool, but they are text matching tools, not syntactical analysers.
Instead, use your presentation language to do that for you.
For example, in C# it is simple:
private bool HasOneCapital(string s)
{
string[] words = s.Split(' ');
foreach (string word in words)
{
if (word.Count(c => char.IsUpper(c)) != 1) return false;
}
return true;
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
modified 6-Mar-22 11:38am.
|
|
|
|
|
I understand you. But it's a test assignment for a position at yandex. I made 5 out of 6 questions easily (had some difficulty with SQL though). It's complicated regarding RegEx as I never used them -- just saw in linux.
|
|
|
|
|
If your position would use regexes, then go away and learn them: if you just get a solution from the internet and then get the interview, you will not be able to explain "your" solution when they ask about it and you will fail at that point as it's obvious you cheated ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Should it not be:
private bool HasOneCapital(string s)
{
string[] words = s.Split(' ');
foreach (string word in words)
{
if (word.Count(c => char.IsUpper(c)) != 1) return false;
}
return true;
} (Closing parenthesis after the "IsUpper(c)" ?)
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Yes, it should ...
Fixed.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello,
I have not worked. with regex for a long time, and I m sure I forgot something simple
In the list below
12334p4p
2345stk
5643stk43
1234db43
1234a1
1234a
I want to capture the first 4 items because the logic to NOT capture is:
Start with 1 to 8 digits
Should have 1 to 3 letters AND 2 or more digit again
I do not want to get the 2 last entries because it finishes with a single letter OR a single letter and a single digit. In other word, if it does finish with 1 letter only OR 1 letter and 1 digit, I do not want to return it
Thank you guys, I'm sure this one is easy !
modified 22-Feb-22 10:08am.
|
|
|
|
|
Did you just order a regex?
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
That's about as basic as regex can get.
Your spec:
. start of string
. 1-8 digits
. 1-3 alpha
. 2-n digits
. end of string
Translate to regex notation (depending on your regex engine, details may vary slightly)
^[0-9]{1,8}[A-Za-z]{1,3}[0-9]{2,}$
Wrap that in your delimiter of choice, and go...
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
modified 22-Feb-22 5:03am.
|
|
|
|
|
Should {1-8} actually be {1,8} ?
|
|
|
|
|
Finger velocity outruns brain function. Yet again...
Fixed, thanks.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I have never been much good at regex, but the detail in your answer makes it easy to understand.
|
|
|
|
|
Thanks. Like many things in coding, writing out your spec unambiguously is most of the battle. Translating it into a programming language or regex or whatever is comparatively routine.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Not that simple, your solution is the first thing I tried, it does not capture the 2 first entries.
Your brain went too fast 😀
|
|
|
|
|
... and I notice you have changed the question. All bets are off until you can describe exactly what you are looking for.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Hello, and hoping this isn't too difficult, as I know once we get the regex, these things work beautifully. I'm not even a novice, I can never figure these complex ones out. But here's the deal.
I have a capture program that captures with timestamp and a filename, which is great. Here is the format they come out as, as an example:
2022.01.30.Sun, 06h36m03s- filename 1.png
2022.01.30.Sun, 06h36m10s- filename 2.png
2022.01.30.Sun, 06h36m16s- filename 3.png
2022.01.30.Sun, 06h36m22s- filename 4.png
The change that is needed is for the timestamp portion above, to be changed from this:
06h36m03s
06h36m10s
to this, if under 9 files:
06h36.1
06h36.2
or to this is over 9 files:
06h36.01
06h36.02
06h36.03
06h36.04
06h36.05
06h36.06
06h36.07
06h36.08
06h36.09
06h36.10
06h36.11
So removined the seconds and just putting in a ".1" or ".01", etc., as needed.
I used to just go to my renamer's forum but it's been dead quite a while now. Hoping someone here can kindly help out.
Thank you in advance!
|
|
|
|
|
A regular expression to match the seconds is trivial: m\d+s .
The problem is that a regular expression works on a single string. There is no way for a pure regex solution to group the files by the start of the name and then apply a sequential number, because the regex only sees one filename at a time, and doesn't maintain any state between invocations.
If you want to do this in code, it should be fairly easy, but you'd need to specify which language you were using.
If you want to use a renamer tool, you'll need to consult the documentation to see if it can do this sort of thing, and if so, how.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard, very sorry for late reply. I'm on a new browser and forgot to put in new bookmark folder for my posts!
Richard, my renamer is good at handling renaming files, it's just the regex part that I dont' know how to deal with. The renamer is fantastic but in one of my crashes, I lost years worth or regex naming files all in one backup file. It was a great loss. And it handled all sorts of difficult things like this. But the renamer's forum is gone so no going back to my posts there to try to rebuild.
All that's needed is the before and after, as it were. How to go from scenarios like this within the file name:
06h36m03s- filename a
06h36m05s- filename b
06h36m12s- filename c
06h36m21s- filename d
06h36m33s- filename e
...
to padded from 1-9 if more than 9
i.e., if above went to 10 and beyond
to:
06h36.1- filename a
06h36.2- filename b
06h36.3- filename c
06h36.4- filename d
06h36.5- filename e
to padded from 1-9 if more than 9:
06h36.01- filename a
06h36.02- filename b
06h36.03- filename c
06h36.04- filename d
06h36.05- filename e
06h36.06- filename f
06h36.07- filename g
06h36.08- filename h
06h36.09- filename i
06h36.10- filename j
06h36.11- filename k
I know it's a tall order but my renamer handled this type of complexity and more! I had files generated by screenshot app and recorded radio programs with all sorts of different naming conventions, and for years I used this app with perfect results. It's just that in the switch to a new computer, and all but 1 backup option, I lost of my years of scripting.
Any help appreciated! Thanks!
|
|
|
|
|
Personally I'm confused by what you say concerning the fabrication of "seconds" from the original set of strings returned. How do you account for the fact that sequentially, 10 (counting ten) tenths adds up to one? "Over 9 files": what the heck does that mean?
In my defense I'd also like to point out that after many years of observing columns of Date Modified, Date Created, Date Accessed windows time stamps (even in MSDOS this is true), what shows is ALWAYS sortable as some "true" time. Despite two "time stamps" of the same look, underneath the msification is a millisecond disposition.
Am not making any sense of this am I?
|
|
|
|
|
I want to be able to use the content of a database table as part of a regex expression. This isn't the actual use case, but it shows what I want to do.
Database table includes a field with customer names, "Acme","Starbucks","Delta". This table gets updated with new customers daily.
I have a simple RegEx that looks for the existence of some text in an OCR'd document. (?i)(Acme|Starbucks|Delta)
I want to update the RegEx statement with the new entries. I know I can do it through some external scripting, but what I would like to do is embed some sort of call to the table and column a the time of execution, like "(?i)(ODBC Hook to the data here)"
|
|
|
|
|
Assuming that the regex engine you are using supports the dotall operator, you can use the following:
'\*\*\* START FAILSAFE \*\*\*\*\*\*\*\*\*(.*?)'\*\*\* STOP FAILSAFE \*\*\*\*\*\*\*\*\*\* Alternatively, you could use
'\*\*\* START FAILSAFE \*\*\*\*\*\*\*\*\*[\w\W]*?'\*\*\* STOP FAILSAFE \*\*\*\*\*\*\*\*\*\*
|
|
|
|
|
I am having a text file having some text to be remove enclosed within a defined tag (234 in the example below). This to-be removed text is not fixed and can occur multiple times in a single line OR can be spanned across multiple lines.
Example -
aaaaaaaaaaa234textA234aaaaa
234text234
blank line
bbbbbb234textB234bbbbbb
ccccc234te
xtC234
dddd234TextD234dddd
With this expression- "(\d)([\s\S]*?)(\d)", I am able to find out and replace the text with "" as expected but the problem occurs wherein the line ends with the tag (234) and the same line either starts with the tag (234) or in the continuation to the previous line like lines 2 and 6 in the above example. The problem is, line 2 and 6 are leaving a blank line behind and so the output contains 7 lines while I am expecting 5 lines (line 2 and 6 should be removed) only.
Please suggest.
|
|
|
|
|
I'd use a text (file) editor and do a replace all.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Hi,
I'm trying to figure how to 'find and replace' a textstring within a textfile. But the find and replace within the file has to only start after a certain textstring and it also has to stop before a second textstring. Can that be done with regex? Or would that require code (Python for instance).
Kind regards,
Roland
|
|
|
|