I'm missing something with this regular expression find/replace attempt. I have the following format:
word | word | word
I would like to first replace every word with "word" to produce
"word" | "word" | "word"
and then subsequently every [space]| with ,, finally producing
"word", "word", "word"
Obviously I could just do this with two simple find(f)/replace(r) commands ( f:([a-z]*\>)r:"$1"; f:[space]|r:,), but is there a way to do all of this at once?
I've tried lots of different ideas, but they all failed. The most successful was finding ([a-z]*\>)(( \|)|\R) and replacing with "$1",, which only ever got me a "word", "word", word format. The solution is probably either much more complicated or much simpler than I'm trying, but I'm stumped. Thanks!
You may use
(\w+)|\s*\|
and replace with (?1"$1":,).
Details
(\w+) - Group 1: one or more word chars
| - or
\s*\| - 0+ whitespaces and then a | char.
(?1"$1":,) - a conditional replacement pattern that replaces with " + Group 1 contents + " if Group 1 matches, else, replaces with ,.
Related
I'm trying to take a query parameter and verify if the syntax provided by the user is correct. Regex seems like the best choice for this, but I'm having trouble making it so the pattern doesn't allow for repeating itself.
The pattern I came up with is:
(^(\w+)(=|!=|>=|>|<=|<|~)((')(.*)('))(\s(AND|OR)\s)(\w+)(=|!=|>=|>|<=|<|~)((')(.*)('))$)
The syntax provided by the user should to be:
[field][predicate][single quote][value][single quote][white space][logical operator][white space][field][predicate][single quote][value][single quote]
Where:
field is [any word]
predicate is [= | != | >= | > | <= | < | ~]
logical operator is [AND | OR (with a space on both sides)]
value is [any word wrapped by single quotes]
An example looks like this: field1='value1' OR field2='value2'
The problem I am having is that the pattern I created allows for things like this:
field1='value1' OR field2='value2field1='value' OR field2='value2'' [This shouldn't work but does]
field1='value1' OR field2='value2 field1='value' OR field2='value2'' [This shouldn't work but does]
field1='value1' OR field2='value2' AND field3='value3' OR field4='value4'' [This shouldn't work but does]
Any help would be appreciated making it so the pattern doesn't match if it repeats.
You might use:
^\w+(?:<=|=>|!=|[~<>=])'\w+'(?: (?:OR|AND) \w+(?:<=|=>|!=|[~<>=])'\w+')*$
^ Start of string
\w+ Match 1 or more word chars
(?: Non capture group
<=|=>|!=|[~<>=] Match one of the alternatives
) Close group
\w+ Match 1 or more word chars between single quotes
(?: Non capture group
(?:OR|AND) \w+ Match space, either AND or OR and 1+ word chars
(?:<=|=>|!=|[~<>=]) Match one of the alternatives
\w+ Match 1 or more word chars between single quotes
)* Close group and repeat 0+ times to also match without AND or OR
$ End of string
If there should be at least a single AND or OR the quantifier of the last group could be + instead of *
The single chars in the predicate could be added to a character class [~<>=] to take out a few alternations.
Regex demo
I have a long file which contains texts and there is a particular type of text which is 24 character long (it can be lowercase alphabet or number),it starts and ends with " and there are parentheses around this.
One of these kind of row looks like this:
"something": ("qwertyuiopasdfghjklz1234"),
and I would like to get:
"something": "qwertyuiopasdfghjklz1234",
I would like to remove the parentheses. I have the following regex: ([a-z0-9"]{26}) which finds this expression but I don't seem to find a way to figure out what to write to replace the row in order to delete the parentheses.
You may use
(:\h*)\(("[a-z0-9]{24}")\)(,)
Replace with $1$2$3, see the regex demo.
Details
(:\h*) - Group 1 ($1): : and 0 or more horizontal whitespaces
\( - a ( char
("[a-z0-9]{24}") - Group 2 ($2): ", 24 lowercase ASCII letters or digits and then a "
\) - a ) char
(,) - Group 3 ($3): a , char.
You may use below code, working for your sample.
Find what: \(|\)
Replace with: nothing
I've got a long google sheets QUERY, part of which is this:
=QUERY(LOOKUP!$A$4:$H,"Select count(B) where UPPER(D) matches 'OK' and UPPER(H) matches '.*(?:^|,|,\s)"®EXEXTRACT(REGEXREPLACE($Q3,"\s|-","")," \w+ ")&"(?:,\s|,|$).*' and (UPPER(C) contains '"®EXEXTRACT($Q3, "\{(\w+)\}")&"' or UPPER(F) contains '"®EXEXTRACT($Q3, "\{(\w+)\}")&"') limit 1 label count(B) ''",0)
Basically if I have an entry like apple {pear}, I only want the apple bit to be matched as part of the query. This works absolutely fine except if I put an & in the bit to match eg. apple&banana {pear}the match fails even though apple&pearis definetely present in the lookup so I think the issue is with my RegEx. I've tried just replacing \w+ selector at the seperated spot in in the RegEx with .* above but no luck.
Any help would be much appreciated
It would make more sense to replace \w+ with \w+(?:&\w+)*.
The \w+(?:&\w+)* pattern matches
\w+ - 1 or more word chars, letters, digits or _
(?:&\w+)* - matches 0 or more occurrences of:
& - a & char
\w+ - 1 or more word chars, letters, digits or _
If you do not want to match _, use
[A-Za-z0-9]+(?:&[A-Za-z0-9]+)*
Note that [A-Za-z0-9&]+ can also be used if you do not care if there are consectuvie & chars in the input (and want to match it).
I have this two lines of text, that I want to manipulate using Regular Expression and substitute:
Obj.FieldNameA = Reader.GetEnumFromInt32<ClassName>(QueryGenerator,nameof(Obj.));
Obj.FieldNameB=Reader.GetTrimmedStringOrNull(QueryGenerator,nameof(Obj.));
Attached on the first Obj. there is a Field name, so in this case they are FieldNameA,FieldNameB
I want to attach these values to the second Obj. found on the same line, so the text should become:
Obj.FieldNameA = Reader.GetEnumFromInt32<ClassName>(QueryGenerator,nameof(Obj.FieldNameA));
Obj.FieldNameB=Reader.GetTrimmedStringOrNull(QueryGenerator,nameof(Obj.FieldNameB));
I have tested this very simple (and wrong) regex:
Obj\.(\w*).*\n
With substituition as $1
But I don't know how to use substitution...
Sample code here
Some Notes:
After FieldNameA there is always an equal sign that could be preceded or followed by a space.
Before the second Obj. there could be any character, including < ( etc...
Could this be achieved?
You may use
Find: (Obj\.(\w+).*\(Obj\.)\)
Replace: $1$2)
See the regex demo.
You may also add ^ to the start of the regex to match only at the start of a line/string.
Details
^ - start of string
(Obj\.(\w+).*\(Obj\.) - Group 1 ($1 in the replacement):
Obj\. - Obj. text
(\w+) - Group 2 ($2): 1 or more word chars
.* - any 0+ chars other than line break chars as many as possible (you may use .*? to only match the second Obj. on a line, your current input only has two with the second one closer to the end of a line, so .* will work better)
\(Obj\. - (Obj. text
\) - a ) char.
I'm using UltraEdit. I have a text file that contains strings like this
Workspace\\Trays\\Dialogs\\Components, Expand, kThisComputerOnly, P_BOOLEAN },
WebCommonDialog Sign_Out, Left, kThisComputerOnly, P_INTEGER_RANGE(0, 4096) },
ThreeDTextDlg, x, kThisComputerOnly, P_INTEGER_RANGE(0, 4096) },
Preferences\\Graphics, CtxDbgMaxGLVersionMajor, kThisComputerOnly, P_INTEGER },
UltraEdit allows PERL, UNIX and UltraEdit style RegEx.
I need to select the second comma and everything to the end of the line and delete it.
Using regexpal.com I've tried several different approaches but can't figure it out.
/,\s.+/ selects the first comma
/[,]\s.+/ same as above
I can't figure out how to select the second command and beyond.
I have also search StackOverflow and found several examples but couldn't change them to work for me.
Thanks.
You may use a Perl regex option with the following pattern:
^([^,]*,[^,]*),.*
and replace with \1.
See the regex demo.
Details:
^ - start of string
([^,]*,[^,]*) - Group 1 (later referred to with \1 backreference from the replacement pattern):
[^,]* - any 0+ chars other than a comma (to prevent overflowing across lines, add \n\r into the negated character class - [^,\n\r]*)
, - a comma
[^,]* - any 0+ chars other than a comma
, - a comma
.* - any 0+ chars other than line break chars as many as possible