question on matching strings

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
CodeLobster

question on matching strings

Post by CodeLobster »

im looking for a way to xyplorer match only the first occurrence of a construction in the format:

"string 1" . "separator" . "string 2" . "separator" . "string 3"

separators will always be the same being ";", "/" or "-" or anything else.

there may or may not be blank spaces after and/or before the construction.

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: question on matching strings

Post by Stefan »

Perhaps something like:


$RESULT = gettoken( sourcestring, occurrence, separator);
$RESULT = formatlist( $RESULT, trim );
makecoffee;


See XY help for exact syntax.

CodeLobster

Re: question on matching strings

Post by CodeLobster »

separator: "/"
expected: "12/7/2011"
source: string.
"this may not happen but at the date 14:44:22 12/7/2011 it happened"

-
Separator: ":"
expected: "14:44:22"
source: now("");

"12/7/2011 14:44:22"

im trying it through regex but not skilled enough on it. the following regex matches only the string 3.

concept:

Code: Select all

$sep = "/";
$match = regexreplace($source, ".+$sep", "");

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: question on matching strings

Post by Stefan »

Your new "expected"
expected: "12/7/2011"
expected: "14:44:22"
looks different as i understood from you first post where i think you want "12" and "14" only.

But now it seams as you have strings like
"Text 12/7/2011 more text 3/9/2011 the end"
or
"Text 14:44:22 more text 18:55:33 the end"
and you want always only the first date or time match?


OK. You want to match:
* one-or-two digits, $SEPARATOR, one-or-two digits, $SEPARATOR, two-or-four digits
* Perhaps with or without leading or trailing text.
* $SEPARATOR could be "/" OR ":"



Test:

Code: Select all

$A = "Get Date: Text 14:44:22 test 12/7/2011 more text 3/9/2011 the end 14:44:22";
  $B = "Get Time: Text 12/7/2011 test 14:44:22 more text 18:55:33 the end 12/7/2011";
    $SEP = ":";
    //$SEP = "/";
    $RegExPattern = ".*?(\d{1,2})$SEP(\d{1,2})$SEP(\d{2,4}).*";
    $RegExReplace = "$1$SEP$2$SEP$3";
    $match = regexreplace( $B , $RegExPattern , $RegExReplace );
    msg $match;
//output:
for string $A >> sep=/ >> "12/7/2011"
for string $B >> sep=: >> "14:44:22


$SEP = "(/|:)";
should work too, but that's to much for me at this hour.

CodeLobster

Re: question on matching strings

Post by CodeLobster »

Stefan wrote:and you want always only the first date or time match?
thats it. there are other specific issues but the general formula you provided will suffice.

the farthest i got was an expresion which was matching dates but then it was eliminating the match which is quite the opposite effect of what i need.

it would be very helpful if you can provide a formula in which several possible separators can be stated then.

hat off, stefan. thanks much on this too.

CodeLobster

Re: question on matching strings

Post by CodeLobster »

so far so good as now i can match "comment-classification-number-357" and other similar common occurrences with variations like upper or lower cases only (classes [a-zA-Z0-9]).

but i cant find a way to match the following:

" \\Machine\X " > being X a specific drive

" path\to\some\file.htm " > sometimes the match has 3 separators

" other\longer path\with variable.extensions " > sometimes 2, or more than 4 but always looking the same: "SPACEfolder 1\(other folders)\file.extensionSPACE", 3 or more digits.

the first case has a regular pattern to match with and the second has a common arrangement as in SPACEfirstBeforeSEP{sep}...{sep}entry{dot}entrySPACE. any formulae here please?

Post Reply