1

I have a variable that contains following characters

"@/$%&*#{}4g_.[]3435_.technology@lte042"

I want to match only "4g_.3435_.technologylte042" by excluding special characters

Code:

set a "@\/$%&*#[](){}4g_.[]3435_.technology@lte042"
regexp {[\w._-]+} $a match
puts $match

I got output :

4g_.3435_.technology

I am not getting remaining characters "lte042"

please help.

gpk
  • 13
  • 1
  • 4

2 Answers2

3

I suppose you are trying to remove all non-word characters, rather than trying to find the first contiguous sequence of word characters. You could use a repeated search-and-replace:

regsub -all {[^\w._-]+} $a "" match

Another option is to use the -all -inline options to produce a list of matches instead of a single match. However, that will put spaces between the successive matches. Eg:

set a "@\/$%&*#[](){}4g_.[]3435_.technology@lte042"
set match [regexp -all -inline {[\w._-]+} $a]
puts $match

==>

4g_.3435_.technology lte042

The second set is necessary because the -inline option doesn't allow match variables to be specified as command parameters.

rici
  • 234,347
  • 28
  • 237
  • 341
  • I tested your code. Still @ is remaining . output was "4g_.3435_.technology@lte042 – gpk Jan 24 '17 at 02:15
  • @gpk: Sorry, missed the "repeated" option, `-all`. Fixed and tested. – rici Jan 24 '17 at 02:17
  • Thanks...It worked...!! Is there a way to get same result using regexp. – gpk Jan 24 '17 at 02:23
  • @gpk: as far as I know, there is not. `regexp` returns one contiguous match, but you want the concatenation of all the matches. You could do that with an explicit loop, but it's easier to erase the parts that don't match, at least in this case. – rici Jan 24 '17 at 02:47
  • @gpk: Added something fairly close, but it might not be exactly what you want. – rici Jan 24 '17 at 03:05
  • One More thing to ask. In above example why there is no any space in case of [] character. Is that considered as word character. – gpk Jan 24 '17 at 03:32
  • @gpk: if I understand what you are asking, it is probably an artifact of TCL list flattening. – rici Jan 24 '17 at 04:14
  • ok.Then how to get result including [] character by regsub or regexp. eg: []4g_.[]3435_.technology lte042 – gpk Jan 24 '17 at 04:44
  • @gpk: In TCL, "a[]b" is the string "ab"; `[]` is an (empty) command substitution so it is substituted by the result of the empty command which is the empty string. If you want a string to include `[`, you need to use `{a[]b}` instead of quotes, in order to prevent the command substitution (which is why you normally have to write regular expressions inside braces instead of quotes). ... An SO comment thread is not an appropriate place for a TCL tutorial, though. If you have specific questions about TCL, ask them as separate questions. – rici Jan 24 '17 at 04:58
  • Use `join` to combine list elements into one string. The optional argument after the list to join is the string to use as the joiner; try the empty string there. – Donal Fellows Jan 24 '17 at 09:07
0

One step closer:

% set s {@/$%&*#{}4g_.[]3435_.technology@lte042}
@/$%&*#{}4g_.[]3435_.technology@lte042
% join [regexp -inline -all {[][\w._-]+} $s] {}
4g_.[]3435_.technologylte042

Documentation: join, regexp, set, Syntax of Tcl regular expressions

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27