I have got dataset like this with three columns
Col1, Col2, Col2
aaa,Arizona DL USTATES,12
bbb,Idaho DL USTATES,35
ccc,Idaho DL USTATES,28
ddd,Wisconsin DL USTATES,11
eeee,Wisconsin DL USTATES,35
What I want to do is that I want to extract the first word of the second column(what is a state name) and put it in the first column.
Expected Output:
Arizona,Arizona randam USTATES,12
Idaho,Idaho randam USTATES,35
Idaho,Idaho randam USTATES,28
Wisconsin,Wisconsin random USTATES,11
The regex that I have is
^[^,]+,([^ ]+) [^\n]+$
With my () I can extract the state name, but How do get the output? What I want is nested parenthesis, something like this
^[^,]+,(([^ ]+) [^\n]+)$
and then the output will be
\1,\2
I should point out that I want to do it using regex replace only.
Edit:
I have solved it by using regex to get all of the state names in a column and then merged it, but I want to know if there are any advanced regex which can be used here.
Your regex with nested parentheses works fine; you just need to use
String‘sreplaceFirstmethod and note that Java uses$for group references. Also note that the groups are enumerated in the order they occur in the regex, so the outer group is$1because it starts first: