Regex ex, first number inside brackets after certain text - regex

I have been trying to get this to work using different tools and sites such as regexr.com but haven't been able to figure it out.
I have a massive block of text somewhere in the text is:
javascript:device_popup(256, 3409)
I am trying to get the 256 sometimes there will be multiple of the above but the number for the comma will always be the same for each block of text.
I have tried variations of the following
/\[(.*?)\]/g
/javascript:device_popup\[(.*?)\]/g
/javascript:device_popup\(\.(*?),)/

Your first attempt matches everything between square brackets. The second attempt does the same only if preceded by "javascript:device_popup". In the third attempt, your syntax is incorrect.
You can modify your regular expression as follows:
/javascript:device_popup\((\d+)/

Related

Regex: Trying to extract all values (separated by new lines) within an XML tag

I have a project that demands extracting data from XML files (values inside the <Number>... </Number> tag), however, in my regular expression, I haven't been able to extract lines that had multiple data separated by a newline, see the below example:
As you can see above, I couldn't replicate the multiple lines detection by my regular expression.
If you are using a script somewhere, your first plan should be to use a XML parser. Almost every language has one and it should be far more accurate compared to using regex. However, if you just want to use regex to search for strings inside npp, then you can use \s+ to capture multiple new lines:
<Number>(\d+\s)+<\/Number>
https://regex101.com/r/MwvBxz/1
I'm not sure I fully understand what you are trying to do so if this doesn't do it then let me know what you are going for.
You can use this find+replace combo to remove everything which is not a digit in between the <Number> tag:
Find:
.*?<Number>(.*?)<\/Number>.*
Replace:
$1
finally i was able to find the right regular expression, I'll leave it below if anyone needs it:
<Type>\d</Type>\n<Number>(\d+\n)+(\d+</Number>)
Explanation:
\d: Shortcut for digits, same as [1-9]
\n: Newline.
+: Find the previous element 1 to many times.
Have a good day everybody,
After giving it some more thought I decided to write a second answer.
You can make use of look arounds:
(?<=<Number>)[\d\s]+(?=<\/Number>)
https://regex101.com/r/FiaTKD/1

Find in Files (Regex) - Returning Consecutive Lines

I need to return all occurrences of when three lines are consecutively written in a file. I'm looking for the following:
FieldName=<some name>
Operator=<some operator>
Value=<some value>
Example File Content
MatchAny=FALSE
FieldValue=TRUE
Operator=Is less than
TotalFields=1
[OutputTarget0SelField0]
FieldName=ORIG-DATE
Operator=Is greater than
Value=20000101
[OutputTarget1]
To do this, I have been trying to use Notepad++ Find in Files functionality but I cannot seem to get the correct regular expression.
Here is what I've tried (in this case I'm assuming the two lines after FieldName= will always be Operator= and Value=)
Find what: (FieldName=|Operator=|Value) is also close, but obviously doesn't account for the fact that these lines need to be consecutive ("FieldName=" followed by "Operator=" followed by "Value=") and returns all single occurrences as well.
You can use ^FieldName=[^\n\r]*[\n\r]+Operator=[^\n\r]*[\n\r]+Value=[^\n\r]* to match your 3 consectuve lines:
^FieldName=[^\n\r]*[\n\r]+ matches a start of a line, follwed by FieldName=, any amount of non-linebreaks and then one or more linbreaks. As you tagged your question with Windows, you might be able to replace [\n\r]+ with \r\n, this also prevents empty lines from jumping into the match (which at the moment would be possible)
Operator=[^\n\r]*[\n\r]+ is basically the same for the Operator-Line
Value=[^\n\r]* is again the same for the Value-Line, this time without the finishing linebreaks
As stated in the comments, this will only show you the first matched line in the find in files overview, but you can double click it, so it shows the whole match.

Extract values from this string?

I have the following string of text.
LOCATION: -20.443 122.951TEMPERATURE: 54.5CCONFIDENCE:
50%SATELLITE: aquaOBS TIME: 2014-05-06T05:30:30ZGRID:
1km
This is being pulled from a feed, and the fieldnames stay the same, but the values differ.
I have been trying to get my head around regular expressions and find a way to pull:
54.5 (temperature)
50 (confidence)
So I need two separate regular expressions that can pull the above from the original string. Any clues or pointers would be great.
I am doing this within a product that allows me to point to strings and can apply regular expressions to the strings so that values can be extracted and written to new fields.
ArcGIS appears to be using a very limited regex engine. It looks like it doesn't even support capturing groups, let alone lookaround. So I guess you need to try the following:
TEMPERATURE: ([0-9.]+)C
will match the TEMPERATURE entry and
CONFIDENCE: ([0-9]+)%
will match the CONFIDENCE entry.
If you're lucky, you can then access the relevant part of the match via the special variable \1 or $1 (which would then contain "54.5" and "50", respectively.
If that's not possible, you'll have to "manually" trim the first 13/12 characters from the left side from the string as well as the rightmost character.
You can split this text with delimiter- new line. As result you get an array. Than you can split the elements of the array with delimiter ':'

Regex with comma separated numbers in Apex

As a preface, I realize there are other topics on regular expressions with comma separated numbers, but when I tried to use those solutions, they didn't work.
Basically, I am trying to create a regular expression to recognize comma separated numbers (in this case without spaces). Before trying to convert this into actual regex syntax, I realize that it should probably work something like this, where 'd' is a number and ',' is a comma, and '+' is a kleene plus:
((d+),)*(d+)
or
(d+)(,(d+))*
Here's the code I am using in an Apex validation to make sure that a certain field is a list of numbers separated by commas without spaces (note: I have tried several variations of this to no avail, but will only post one):
(\d+,)*(\d+)
For some reason this isn't working, but it seems to be the correct syntax of any digit 1 or more times followed by a single comma, and that entire expression can be repeated 0 or more times, and that entire repeated expression should always be followed by at least 1 digit.
This expression in practice does recognize all the accepted forms (ex: 100 or 100,200 etc.), but for some reason it also accepts answers like
'100,200,'
or
'100,200,,'
or
'100,,200'
I'm pretty stumped as to why this won't work as well as the previously given solutions which seem to do the same thing mine do. Thanks for any help in advance!
That's it:
^(\d+,)*\d+$
The anchors ^$ will make the difference because they will force the whole string (not just a part) to match the pattern
You should try pattern like this:
^(?:(\d+),)+(\d)+$

.net/powershell regex single match

I have spent days trying to work this out. Managed to get text to be captured but I need only one of the lines.
I have tried various ways but always get all matches returned to me.
This line of text appears 3 times
<![LOG[Property SerialNumber is now = serial]LOG]!>
using the Regex
(?<=Property\sSerialNumber\sis\snow\s\=\s)[^<]+(?=]LOG]!>)
I get three matches of the word serial. I only need 1.
Where am i going wrong?
If that exact line appears three times, then [Regex]::Matches will return all three of them, of course.
You can use [Regex]::Match if you're only interested in the first.