How to get specific data from String using regex in java? [duplicate] - regex

This question already has answers here:
how can I exctract attribute value using JAVA regex
(2 answers)
Closed 4 years ago.
I have a string.
String str= " <decision CCDBNUM=\"1111111\" adddate=\"20180112\"><decision CCDBNUM=\"2222222\" adddate=\"20180114\"> ";
I want to write a regex to fetch a particular value from this string.
My Expected Output is: to fetch only the value of CCDBNUM, i.e,
1111111 2222222
Please help me with this issue.

This should work:
CCDBNUM="([^"]+)"
Just get group 1 of each match.
I assume that CCDBNUM wouldn't contain the characters CCDBNUM. If that's not the case, I suggest you use an XML parser. Regex is not enough for that.
Demo

Related

Remove last character from regex match [duplicate]

This question already has answers here:
How can I match "anything up until this sequence of characters" in a regular expression?
(15 answers)
Closed 3 years ago.
I have strings that are concatenations of airline codes/flightnumbers, separated with ;. The airline code and flight number are either separated by a space or -. So some examples are:
AA-1234;UA 243;EK 23;
9W 23;B6-134
Now I want to grab the airline codes from this.
I came up with the following regex: [a-zA-Z0-9]{2}[ -]. This works to grab the airline codes but also includes the airlinecode-flightnumber separator. How would I adjust my regex to not include this?
[a-zA-Z0-9]{2}(?=[ -])
See it in action here

Regular expression splunk query [duplicate]

This question already has answers here:
Getting the text that follows after the regex match
(5 answers)
Closed 4 years ago.
I have a line containing
[India,sn_GB] Welcome : { Name:{Customer1},Place:{Mumbai},}
I want to print the entire line after sn_GB] in splunk, which is
Welcome : { Name:{Customer1},Place:{Mumbai},}
I used the below regular expression:
(?<=sn_).*?$
But it prints, along with GB] like GB] Welcome : { Name:{Customer1},Place:{Mumbai},}.
In the word sn_GB, sn_ is constant and the rest two letter will vary, like GB, LB, KB, TB as such.
Please help me in correcting the regular expression.
Thanks
This will give the correct result in case sn_GB is constant.
(?<=sn_GB).*?$
If GB is not constant you can go for:
(?<=sn_...).*?$
I understand your question now.
Country codes are always 2 letters.
i'd use
(?<=sn_..\]\ ).*$
but you could use
(?<=sn_[A-Z]{0,5}\]\ \s*).*?$
(?<=sn_....).*$
is the simplest, as it will just grab 4 characters after, if it's always 2 letters for country code, and then a closing bracket and a space

Select lines not containing phrase [duplicate]

This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 6 years ago.
I have several lines win one word in each in my Notepad++. How to select all words not not containing Set ant Get phrase.
SetBlaBla1
BlaGetBla2
BlaBla3
Result should be BlaBla3
Try negative look-ahead, something like this:
^((?!Set|Get).)*$

reformat numbers using regex [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I have a file that contains any of the following number format
12.456.7890
12-456-7890
123.456.7890
(123)456.7890
(123).456.7890
123-456-7890
(123)-456-7890
(123)456-7890
Is it possible to use regex substitution so that the final output number will always be on a format (123)456-7890 or (12)456-7890
Yes, it is:
s/\(?(\d\d\d)\)?[-.]?(\d\d\d)[-.]?(\d\d\d\d)/($1)$2-$3/g
I should mention that the above will also parse the following two:
123)456.7890
(123456.7890
You can do this using two substitutions:
perl -lpe 's/\D//g; s/(\d{3})(\d{3})(\d{4})/($1)$2-$3/' file
The first one removes all characters that aren't numeric. The second one inserts the desired characters between each group.
You should take into account that this approach will make a mess of any lines that aren't like the ones in your sample input. One means of protecting yourself could be something like this:
if ((#a = /\d/g ) == 10) { /* perform substitutions */ }
i.e. ensure that the number of matches on the line is 10 before proceeding.

Regular expression for currency finding from the text of line [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular expression for finding currency values but not dates in text
Hi this is my text can u please provide reg ex for finding currency only as shown below:
97.38
86.16
3,259.81
28,781.07
problem here is it is getting value from date also because it is in the of the currency so it should restrict the value from date
Ex text:
13/07/2011 EA35906558 - 13.07.11 8054 97.38
14/07/2011 EA35906566-14.07.11 8054 86.16
14/08#011 VP40853570 - 14.08.11 8122 3,259.81
14108/2011 VP50433270-14.08.11 8122- 28,781.07
Are the numbers always at the end of the line? If so:
\s([0-9]+\,?[0-9]+\.[0-9]+)$
http://rubular.com/r/8z7r8epLk9
That is actually very easy:
resultString = Regex.Match(subjectString, #"([\d.,]+)\s*$").Groups[1].Value;
Provided you care only for the last number of the string. If you apply the above in each line you will get your currency. No need for special format or anything. Implementation is .NET but you can change it to anything you want.