how to form a regex that will parse the following? - regex

I need to parse the following line:
Action(X,X,Cash(50))Action(Y,Y,Material(30,Car,2))Action(I,I,Cash(50))
The output should look like:
Action(X,X,Cash(50))
Action(Y,Y,Material(30,Car,2))
Action(I,I,Cash(50))
The regex I used is:
String tokenRegex = "(Action+\\(([a-zA-Z]+|\\,|([a-zA-Z]+\\(\\d*|[a-zA-Z]+|\\,)\\))+\\))";
It fails to parse "Action(Y,Y,Material(30,Car,2))" but works for "Action(X,X,Cash(50))".
What am i doing wrong. What will be the correct regex?

I think this does it:
String tokenRegex = "(Action\\([a-zA-Z]+,[a-zA-Z]+,[a-zA-Z]+\\(\\d+(,([a-zA-Z]+|\\d+))*\\)\\))";
I removed some the parentheses that weren't needed for grouping in the regular expression. If you need them for capturing parts of the expression, you'll have to add them back.

Instead of using a regex just do something to the effect of
string.replace(")A", ")\nA");

Related

Regex Expression - text between quotes and brackets

i have the following JSON string that i need to parse:
{'ConnectionDetails':'{\'server\':\'johnssasd02\',\'database\':\'enterprise analytics\'}'}]}
i am already using the expression '([^']*)' to get everything in quotes, which correctly gets me the ConnectionDetails title. However i now need an expression to get me everything between '{ and '} in order to get the full path value. so i need to capture the following from above string:
{\'server\':\'johnssasd02\',\'database\':\'enterprise analytics\'}
but having trouble coming up the regex expression
thanks
In order to extract the data between the curly braces {} you can use the regex: \{(.*?)\}
i accomplished it within an SSIS derived column task where i removed unwanted characters from the input string. that way i don't have to worry about dealing with them using regex.

regular expression replace removes first and last character when using $1

I have string like this:
&breakUp=Mumbai;city,Puma;brand&
where Mumbai;city and Puma;brand are filters(let say) separated by comma(,). I have to add more filters like Delhi;State.
I am using following regular expression to find the above string:
&breakUp=.([\w;,]*).&
and following regular expression to replace it:
&breakUp=$1,Delhi;State&
It is finding the string correctly but while replacing it is removing the first and last character and giving the following result:
&breakUp=umbai;city,Puma;bran,Delhi;State&
How to resolve this?
Also, If I have no filters I don't want that first comma. Like
&breakUp=&
should become
&breakUp=Delhi;State&
How to do it?
My guess is that your expression is just fine, there are two extra . in there, that we would remove those:
&breakUp=([\w;,]*)&
In this demo, the expression is explained, if you might be interested.
To bypass &breakUp=&, we can likely apply this expression:
&breakUp=([^&]+)&
Demo
Your problem seems to be the leading and trailing period, they are matched to any character.
Try using this regex:
&breakUp=([\w;,]*)&

How to match String with regex in this case?

I have get a set of Strings with pattern like:
category=50025969&city=%CE%C2%D6%DD&auction_start_seg=-1
now I wish to extract all substrings like:
city=%CE%C2%D6%DD
How can I write a regex to express that?
there's a lot of possible regex to answer your question. Here is mine:
(?=city=)[^&]*
gives you city=VALUE where match starts in city= until it finds a & char.

How can I use regex to replace numbers in my regular expressions inside notepad++?

I have the following:
<d:RowKey>XXXX004K<
replace with
<d:RowKey>050505004K<
and
RowKey='XXXX004K'
replace with
RowKey='0505050004K'
Can someone help me with the regular expression syntax for these. The value of "X" in the above can be any number.
Sorry but I have never used regex before. I just need some way to do this inside notepad++ where it gives me an option of regex for a search string.
Tried with a simple string
string pattern = #"\d{4}004K";
string search = "<d:RowKey>8989004K<";
var result = Regex.Replace(search, pattern, "050505004K");
Try
Find What: ([>'])[0-9][0-9][0-9][0-9]
Replace With : \1050505
Assuming 05050504K< there is a typo in here

Matching single or double quoted strings in Vim

I am having a hard time trying to match single or double quoted strings with Vim's
regular expression engine.
The problem is that I am assigning the regular expression to a variable and then using that
to play with matchlist.
For example, let's assume I know I am in a line that contains a quoted string and I want to match it:
let regex = '\v"(.*)"'
That would work to match anything that is double-quoted. Similarly, this would match single quoted strings:
let regex = "\v'(.*)'"
But If I try to use them both, like:
let regex = '\v['|"](.*)['|"]'
or
let regex = '\v[\'|\"](.*)[\'|\"]'
Then Vim doesn't know how to deal with it because it thinks that some quotes are not being closed in the actual variable definition and it messes up the regular expression.
What would be the best way to catch single or double quoted strings with a regular expression?
Maybe (probably!) I am missing something really simple to be able to use both quotes and not worry about the surrounding quotes for the actual regular expression.
Note that I prefer single quotes for regular expression because that way I do not need to double-backslash for escaping.
You need to use back references. Like so:
let regex = '\([''"]\)\(.\{-}\)\1'
Or with very-magic
let regex = '\v([''"])(.{-})\1'
Alternatively you could use (as it will not mess with your sub-matches):
let regex = '\%("\([^"]*\)"\|''\([^'']*\)''\)'
or with very magic:
let regex = '\v%("([^"]*)"|''([^'']*)'')'
look at this post
Replacing quote marks around strings in Vim?
might help in some way
This is a workable script I write for syntax the quoted strings.
syntax region myString start=/\v"/ skip=/\v(\\[\\"]){-1}/ end=/\v"/
syntax region myString start=/\v'/ end=/\v'/
You may use \v(\\[\\"]){-1} to skip something.